7,759
社区成员
发帖
与我相关
我的任务
分享
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 File As String
Public Sub DeleteSection(ByVal Section As String) '删除节的过程
Dim ret As Integer
ret = WritePrivateProfileString(Section, 0&, "", File)
End Sub
Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)
'保存数据的函数
SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)
End Function
Public 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 If
End Function
Public Property Get filename() As String
filename = File
End Property
Public 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 Long
Private Sub Form_Load()
Dim tmp As String
tmp = Space(255)
GetPrivateProfileString "Settings", "homePage", "", tmp, 255, App.Path & "\config.ini"
Label1 = tmp
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Save settings
WritePrivateProfileString "Settings", "homePage", Label1.Caption, App.Path & "\config..ini"
End Sub