Public Function IniGetAllSection(iniFileName As String, Section() As String) As Long '读取INI文件所有节 On Error GoTo er Dim Length As Long, pos As Integer, Count As Integer buffer = String(32767, 0) Length = GetPrivateProfileString(vbNullString, vbNullString, "", buffer, Len(buffer), iniFileName) buffer = Left(buffer, Length) Do ReDim Preserve Section(Count) pos = InStr(buffer, Chr(0)) If Left(buffer, pos - 1) = "" Then Exit Do Section(Count) = Left(buffer, pos - 1) buffer = Mid(buffer, pos + 1) If Len(buffer) <= 0 Then Exit Do Count = Count + 1 Loop IniGetAllSection = Count Exit Function er: ReDim Section(0) IniGetAllSection = 0 End Function
用这个不行?但我不知道怎么用: 读取 某一个 Section 之所有资料 Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) 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 LongPublic Function GetKeyNames(ByVal iniFileName As String, ByVal iniSectionName As String, ByRef iniKeyNames() As String, Optional ByVal lngBufferSize As Long = &HFFFF&) As Long Dim strKeys As String, lngRetVal As Long If lngBufferSize <= 0& Then Exit Function getkeynames_getnamesagain: strKeys = String$(lngBufferSize, vbNullChar) lngRetVal = GetPrivateProfileString(iniSectionName, ByVal 0&, "*", _ strKeys, lngBufferSize, iniFileName) If lngRetVal >= lngBufferSize - 2& Then 'buffer too small, attempts to realloc more space lngBufferSize = lngBufferSize + lngBufferSize GoTo getkeynames_getnamesagain ElseIf lngRetVal Then 'ok iniKeyNames = Split(Left$(strKeys, lngRetVal - 1&), vbNullChar) GetKeyNames = UBound(iniKeyNames) + 1& Else 'no keys or error occured GetKeyNames = 0& End If End Function ' 传入ini文件名、section名,和接受key名称的数组。 ' 返回section里key的数量,iniKeyNames为包含key名称的数组' 例子 ' Dim tsNames() As String, i As Long, n As Long ' n = GetKeyNames("test.ini", "section", tsNames) ' For i = 0& To n - 1& ' msgbox tsnames(i) ' Next ' End With
'读取INI文件所有节
On Error GoTo er
Dim Length As Long, pos As Integer, Count As Integer
buffer = String(32767, 0)
Length = GetPrivateProfileString(vbNullString, vbNullString, "", buffer, Len(buffer), iniFileName)
buffer = Left(buffer, Length)
Do
ReDim Preserve Section(Count)
pos = InStr(buffer, Chr(0))
If Left(buffer, pos - 1) = "" Then Exit Do
Section(Count) = Left(buffer, pos - 1)
buffer = Mid(buffer, pos + 1)
If Len(buffer) <= 0 Then Exit Do
Count = Count + 1
Loop
IniGetAllSection = Count
Exit Function
er:
ReDim Section(0)
IniGetAllSection = 0
End Function
读取 某一个 Section 之所有资料
Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Dim strKeys As String, lngRetVal As Long
If lngBufferSize <= 0& Then Exit Function
getkeynames_getnamesagain:
strKeys = String$(lngBufferSize, vbNullChar)
lngRetVal = GetPrivateProfileString(iniSectionName, ByVal 0&, "*", _
strKeys, lngBufferSize, iniFileName)
If lngRetVal >= lngBufferSize - 2& Then 'buffer too small, attempts to realloc more space
lngBufferSize = lngBufferSize + lngBufferSize
GoTo getkeynames_getnamesagain
ElseIf lngRetVal Then 'ok
iniKeyNames = Split(Left$(strKeys, lngRetVal - 1&), vbNullChar)
GetKeyNames = UBound(iniKeyNames) + 1&
Else 'no keys or error occured
GetKeyNames = 0&
End If
End Function
' 传入ini文件名、section名,和接受key名称的数组。
' 返回section里key的数量,iniKeyNames为包含key名称的数组' 例子
' Dim tsNames() As String, i As Long, n As Long
' n = GetKeyNames("test.ini", "section", tsNames)
' For i = 0& To n - 1&
' msgbox tsnames(i)
' Next
' End With