7,762
社区成员
发帖
与我相关
我的任务
分享
'声明API函数
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 iniFile As String
Private iniSec As String
Private iniKey As String
'-------------------------------------
Public Property Let FileName(strfile As String)
If Right(strfile, 4) <> ".ini" Then
strfile = strfile & ".ini"
End If
iniFile = strfile
End Property
Public Property Get FileName() As String
FileName = iniFile
End Property
Public Property Let Section(strSection As String)
iniSec = strSection
End Property
Public Property Get Section() As String
Section = iniSec
End Property
Public Property Let Key(strKey As String)
iniKey = strKey
End Property
Public Property Get Key() As String
Key = iniKey
End Property
'读取INI文件中的条目
Public Function ReadItem(Optional ByVal strSection As String, Optional ByVal strKey As String)
Dim sTemp As String * 256
Dim nLen As Integer
sTemp = Space$(256)
'如果没有指定参数则按属性设置进行
If Len(strSection) > 0 Then
iniSec = strSection
End If
If Len(strKey) > 0 Then
iniKey = strKey
End If
nLen = GetPrivateProfileString(iniSec, iniKey, vbNullString, sTemp, 255, iniFile)
ReadItem = Left$(sTemp, nLen)
End Function
'将指定信息写入INI文件
Public Function WriteItem(ByVal value As String, Optional ByVal strSection As String, Optional ByVal strKey As String)
Dim x As Long, Buff As String * 256, i As Integer
'INI文件中的字符串必须以字符CHr(0)结尾
Buff = value + Chr(0)
If Len(strSection) > 0 Then
iniSec = strSection
End If
If Len(strKey) > 0 Then
iniKey = strKey
End If
x = WritePrivateProfileString(iniSec, iniKey, Buff, iniFile)
If x = 0 Then MsgBox "ini文件写入错误!", , ""
End Function