新建From1(窗体),新建Command1(按钮CommandButton),代码:
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 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 Function f_ReadINI(ByVal strApplicationName As String, ByVal strKeyName As String) As String
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString(strApplicationName, strKeyName, "", buff, 256, App.Path & "\Config.ini")
f_ReadINI = buff
End Function
Private Function f_WriteINI(ByVal strApplicationName As String, ByVal strKeyName As String, ByVal strReturnedString As String) As Long
On Error Resume Next
f_WriteINI = WritePrivateProfileString(strApplicationName, strKeyName, strReturnedString, App.Path & "\Config.ini")
End Function
Private Sub Command1_Click()
Debug.Print "Write " & f_WriteINI("1", "2", "3")
Debug.Print "Read " & f_ReadINI("1", "2")
End Sub
运行结果:
Write 1
Read 3
新建了文件Config.ini,记事本打开结果:
[1]
2=3