读书人

VB配置文件调用有关问题-新手

发布时间: 2012-01-19 00:22:27 作者: rapoo

VB配置文件调用问题--新手
配置文件
[system]
keyword1={abc}def
keyword2=ghi
使用
Private Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Dim keyword1 As String
keyword1 = String(255, 0)
GetPrivateProfileString "system ", "keyword1 ", "没有配置 ", keyword1,
255, "c:\system.ini "
Dim keyword2 As String
keyword2 = String(255, 0)
GetPrivateProfileString "system ", "keyword2 ", "没有配置 ", keyword2,
255, "c:\system.ini "
keyword3 = Replace(keyword1, "{abc} ", keyword2)
然后想把{abc}替换成keyword2的内容
现在遇到问题是替换之后的结果是ghi,keyword1后面的def没有了

[解决办法]
Private Declare Function GetPrivateProfileString Lib "kernel32 " Alias "GetPrivateProfileStringA " (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Sub Command1_Click()
Dim keyword1 As String, keyword2 As String, keyword3 As String

keyword1 = ReadINIFile( "C:\ ", "System.ini ", "System ", "keyword1 ")
keyword2 = ReadINIFile( "C:\ ", "System.ini ", "System ", "keyword2 ")
keyword3 = Replace(keyword1, "{abc} ", keyword2)
End Sub

'Read INI file
Public Function ReadINIFile(ByVal sFilePath As String, ByVal sFileName As String, ByVal sSection As String, ByVal sKey As String, _
Optional ByVal sDefault As String = " ") As String
Dim intLen As Integer, strRet As String
Dim strPath As String

On Error Resume Next

strPath = sFilePath
If Right(strPath, 1) <> "\ " Then strPath = strPath & "\ "
strRet = Space(255)
intLen = GetPrivateProfileString(sSection, sKey, sDefault, _
strRet, Len(strRet), strPath & sFileName)

ReadINIFile = Left(strRet, intLen)
End Function

读书人网 >VB

热点推荐