试过了下面这个函数,2000/xp下都是没问题的,98下就不行了?还有没有好点的函数?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
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
test.ini文件
[Open]
FileName1=E:\MSSQL7\movie\beyond\a\MUSIC04.DAT
FileName2=E:\MSSQL7\movie\beyond\a\MUSIC02.DAT
FileName3=E:\MSSQL7\movie\beyond\a\MUSIC03.DATPrivate Sub Command1_Click()
Dim tsNames() As String, i As Long, n As Long
n = GetKeyNames(App.Path & "\test.ini", "open", tsNames)
For i = 0& To n - 1&
MsgBox tsNames(i)
Next
End Sub