ReadSections(Strings: TStrings)
其中Strings存放的就是a,aa
2. 读取某个Section中的值
ReadSectionValues('Section 值', Strings: TStrings)
如:ReadSectionValues('aa', Strings),Strings中存放的就是a,b的值
'利用API函数批量读取某个 节 所有项目Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'对API抱装了一下,参数 1.节名; 2.文件名(包括路径)。返回字符串,每个项目以英文逗号分隔。
Public Function getProfileSection(ByVal lpAppName As String, ByVal lpFileName As String) As String
If Dir(lpFileName) <> "" Then
Dim buffer As String
buffer = Space(FileLen(lpFileName))
If GetPrivateProfileSection(lpAppName, buffer, Len(buffer), lpFileName) > 0 Then
Dim loc As Integer
loc = InStr(buffer, Chr(0) & Chr(0))
buffer = Left(buffer, loc - 1)
buffer = Replace(buffer, Chr(0), ",")
getProfileSection = buffer
End If
End If
End Function
'这个函数用上面的函数获取ini文件中的串,把它转换为两维数组返回,如读取错误返回null
Public Function ProfileSectionToArray(ByVal Section As String) As Variant
On Error GoTo errHandle
Dim a
a = Split(Section, ",")
Dim ar() As String
ReDim ar(UBound(a), 1)
Dim i As Integer
Dim items
For i = 0 To UBound(a)
items = Split(a(i), "=")
ar(i, 0) = items(0)
ar(i, 1) = items(1)
Next
ProfileSectionToArray = ar
Exit Function
errHandle:
ProfileSectionToArray = Null
End Function
'这是测试
Private Sub Command1_Click()
Dim keyvalues As String
keyvalues = getProfileSection("舞台灯光", "c:\123.ini")
Dim ar
ar = ProfileSectionToArray(keyvalues)
If Not IsNull(ar) Then
Dim i As Integer
For i = 0 To UBound(ar, 1) '遍历二维数组
Debug.Print ar(i, 0), ar(i, 1)
Next
End If
End Sub
'一次只能读取一个节,如果要把几个节的项目合在一个数组中,可以多getProfileSection几次连接成一个字符串,然后再ProfileSectionToArray
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)