代码中的 Settings 是 ini 文件中用方括号括起来的应用名。这个根据你的情况去改。 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 LongPrivate Sub Form_Load() Dim tmp As String tmp = Space(255) GetPrivateProfileString "Settings", "homePage", "", tmp, 255, App.Path & "\config.ini" Label1 = tmp End SubPrivate Sub Form_Unload(Cancel As Integer) 'Save settings WritePrivateProfileString "Settings", "homePage", Label1.Caption, App.Path & "\config..ini" End Sub
建一个类模块..代码 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 LongPrivate 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 LongPrivate File As StringPublic Sub DeleteSection(ByVal Section As String) '删除节的过程 Dim ret As Integer ret = WritePrivateProfileString(Section, 0&, "", File) End SubPublic Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$) '保存数据的函数 SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File) End FunctionPublic Function GetSetting(ByVal Section As String, _ ByVal KeyName As String) As String '取得数据的函数 Dim ret As Integer Dim t As String * 1000 ret = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File) '注意默认值为"unknown value",也可设为"" If ret > 0 Then GetSetting = Left$(t, ret) Else GetSetting = "Unknown section or key" '出错则返回"Unknown section or key" End IfEnd FunctionPublic Property Get filename() As String filename = File End PropertyPublic Property Let filename(ByVal NewFileName As String) File = NewFileName End Property 然后下面是调用示例 IniUtil.GetSetting("config", "homePage") '这个是读取, IniUtil.SaveSetting("config", "homePage" ,"保存值") '这个保存 iniutil是实例名,需要new出来后的.
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 LongPrivate Sub Form_Load()
Dim tmp As String tmp = Space(255)
GetPrivateProfileString "Settings", "homePage", "", tmp, 255, App.Path & "\config.ini"
Label1 = tmp
End SubPrivate Sub Form_Unload(Cancel As Integer)
'Save settings
WritePrivateProfileString "Settings", "homePage", Label1.Caption, App.Path & "\config..ini"
End Sub
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 LongPrivate 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 LongPrivate File As StringPublic Sub DeleteSection(ByVal Section As String) '删除节的过程 Dim ret As Integer ret = WritePrivateProfileString(Section, 0&, "", File)
End SubPublic Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)
'保存数据的函数
SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)
End FunctionPublic Function GetSetting(ByVal Section As String, _
ByVal KeyName As String) As String '取得数据的函数 Dim ret As Integer Dim t As String * 1000 ret = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File) '注意默认值为"unknown value",也可设为"" If ret > 0 Then
GetSetting = Left$(t, ret)
Else
GetSetting = "Unknown section or key" '出错则返回"Unknown section or key"
End IfEnd FunctionPublic Property Get filename() As String
filename = File
End PropertyPublic Property Let filename(ByVal NewFileName As String)
File = NewFileName
End Property
然后下面是调用示例 IniUtil.GetSetting("config", "homePage") '这个是读取,
IniUtil.SaveSetting("config", "homePage" ,"保存值") '这个保存
iniutil是实例名,需要new出来后的.