http://blog.csdn.net/laotou99/article/details/8574265
http://ersong1.blog.163.com/blog/static/2327713720110203194176/
代码最开头加入这个:
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
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 Const mc_strIniFileName As String = "MyIni.ini"
然后添加这个:
Public Function GetIni(appName As String, keyName As String) As String
Dim strDefault As String
Dim lngBuffLen As Long
Dim strResu As String
Dim x As Long
Dim strIniFile As String
If Right(App.Path, 1) = "\" Then
strIniFile = App.Path & mc_strIniFileName
Else
strIniFile = App.Path & "\" & mc_strIniFileName
End If
strResu = String(1025, vbNullChar): lngBuffLen = 1025
strDefault = ""
x = GetPrivateProfileString(appName, keyName, strDefault, strResu, lngBuffLen, strIniFile)
Debug.Print x
Debug.Print strResu
GetIni = Left(strResu, x)
End Function
Public Sub WriteIni(appName As String, keyName As String, valueNew As String)
'
Dim x As Long
Dim strIniFile As String
If Right(App.Path, 1) = "\" Then
strIniFile = App.Path & mc_strIniFileName
Else
strIniFile = App.Path & "\" & mc_strIniFileName
End If
x = WritePrivateProfileString(appName, keyName, valueNew, strIniFile)
Debug.Print x
End Sub
然后一个button测试下:
Private Sub Command3_Click()
Dim strRet As String
strRet = GetIni("AAA", "BBB")
MsgBox "读出为:" & strRet
End Sub