读写ini文件的类
糊涂无品 2020-12-20 01:37:46 Option Explicit
Private strInI As String
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 Any, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Function MakePath(ByVal strDrv As String, _
ByVal strDir As String) As String
Do While Right$(strDrv, 1) = ""
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop
Do While Left$(strDir, 1) = ""
strDir = Mid$(strDir, 2)
Loop
MakePath = strDrv & "" & strDir
End Function
Public Sub CreateIni(strDrv As String, strDir As String)
strInI = MakePath(strDrv, strDir)
End Sub
Public Sub WriteFile(strSection As String, _
strKey As String, _
strValue As String)
WritePrivateProfileString strSection, _
strKey, strValue, strInI
End Sub
Public Function GetFile(strSection As String, _
strKey As String) As String
Dim strTmp As String
Dim lngRet As String
strTmp = String$(100, Chr(32))
lngRet = GetPrivateProfileString(strSection, _
strKey, "", strTmp, _
Len(strTmp), strInI)
GetFile = strTmp
End Function
Public Property Let INIFile(ByVal New_IniPath As String)
strInI = New_IniPath
End Property
Public Property Get INIFile() As String
INIFile = strInI
End Property
Public Sub getAllSection(strArray() As String)
Dim strTmp As String
Dim lngRet As String
strTmp = Space(256)
lngRet = GetPrivateProfileString(0&, _
0&, "", strTmp, _
256, strInI)
strTmp = Left(strTmp, InStr(1, strTmp, Chr(0) & Chr(0)) - 1)
strArray = Split(strTmp, Chr(0))
End Sub
Public Sub getAllKey(strSection As String, strArray() As String)
Dim strTmp As String
Dim lngRet As String
strTmp = Space(256)
lngRet = GetPrivateProfileString(strSection, _
0&, "", strTmp, _
256, strInI)
strTmp = Left(strTmp, InStr(1, strTmp, Chr(0) & Chr(0)) - 1)
strArray = Split(strTmp, Chr(0))
End Sub