'读ini字符串 Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String, ByVal FileName As String) As String Dim ResultString As String * 144, Temp As Integer Dim s As String, i As Integer Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, FileName) '检 索 关 键 词 的 值 If Temp% > 0 Then '关 键 词 的 值 不 为 空 s = "" For i = 1 To 144 If Asc(Mid$(ResultString, i, 1)) = 0 Then Exit For Else s = s & Mid$(ResultString, i, 1) End If Next Else Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, FileName) '将 缺 省 值 写 入 INI 文 件 s = DefString End If GetIniS = s End Function '读ini数值 Public Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Integer, ByVal FileName As String) As Integer Dim d As Long, s As String d = DefValue GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, FileName) If d <> DefValue Then s = "" & d d = WritePrivateProfileString(SectionName, KeyWord, s, FileName) End If End Function '写ini字符串 Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String, ByVal FileName As String) Dim res% res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, FileName) End Sub '写ini数值 Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Integer, ByVal FileName As String) Dim res%, s$ s$ = str$(ValInt) res% = WritePrivateProfileString(SectionName, KeyWord, s$, FileName) End Sub
我能读取ini文件,但不知道怎么样遍历该文件
我能读取ini文件,但不知道怎么样遍历该文件
Private Declare Function ProfileGetAllSectionName Lib "kernel32" Alias "GetPrivateProfileStringA" ( _ ByVal lpApplicationName As Long, _ ByVal lpszKey As Long, _ ByVal lpszDefault As String, _ ByVal lpszReturnBuffer As String, _ ByVal cchReturnBuffer As Long, _ ByVal lpszFile As String _ ) As Long'Return Sections Name in Collection Public Function AllSection() As Collection On Error GoTo AllSection_Error
Dim sReturn As String Dim iFound As Integer Dim i As Integer Dim iNullOffSet As Integer Dim varAllSection As New Collection
Do iNullOffSet = InStr(sReturn, Chr$(0)) If iNullOffSet > 1 Then varAllSection.Add Mid$(sReturn, 1, iNullOffSet - 1) sReturn = Mid$(sReturn, iNullOffSet + 1) End If Loop While iNullOffSet > 1 Set AllSection = varAllSection GoTo WayOut AllSection_Error: WayOut: Set varAllSection = Nothing End Function
Private Declare Function ProfileGetAllKeysName Lib "kernel32" Alias "GetPrivateProfileStringA" ( _ ByVal lpApplicationName As String, _ ByVal lpszKey As Long, _ ByVal lpszDefault As String, _ ByVal lpszReturnBuffer As String, _ ByVal cchReturnBuffer As Integer, _ ByVal lpszFile As String _ ) As Long'Return Keys Name in Collection Public Function AllKey _ (Optional ByVal isSection As String) _ As Collection
On Error GoTo AllKey_Error Dim sReturn As String Dim iFound As Integer Dim i As Integer Dim iNullOffSet As Integer
Dim varAllKey As Collection
Set varAllKey = New Collection
isSection = Trim$(isSection) If isSection = "" Then isSection = DefaultSection End If sReturn = String$(255, 0) Call ProfileGetAllKeysName(isSection, 0, "", sReturn, 255, FileFullName)
Do iNullOffSet = InStr(sReturn, Chr$(0)) If iNullOffSet > 1 Then varAllKey.Add Mid$(sReturn, 1, iNullOffSet - 1) sReturn = Mid$(sReturn, iNullOffSet + 1) End If Loop While iNullOffSet > 1 DefaultSection = isSection
Set AllKey = varAllKey GoTo WayOut AllKey_Error: WayOut: Set varAllKey = Nothing End Function
一个按钮,两个listbox '注意引用microsoft scripting runtime对象 Option ExplicitDim arrSection() As String, arrkey() As String Private Sub Command1_Click() Dim FSO As New FileSystemObject Dim tt As TextStream Set tt = FSO.OpenTextFile("d:\mc\win.ini") Dim temps As String Dim s As String Dim i As Long Dim j As Long Dim arrtemp As Variant i = 0 Do While Not tt.AtEndOfStream s = Trim(tt.ReadLine) arrtemp = Split(s, "=") j = UBound(arrtemp) If j = 1 Then ReDim Preserve arrSection(i) ReDim Preserve arrkey(i) arrSection(i) = Trim(arrtemp(0)) List1.AddItem arrSection(i) arrkey(i) = Trim(arrtemp(1)) List2.AddItem arrkey(i) i = i + 1 End If Loop Set tt = Nothing Set FSO = Nothing End Sub
Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String, ByVal FileName As String) As String
Dim ResultString As String * 144, Temp As Integer
Dim s As String, i As Integer
Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, FileName)
'检 索 关 键 词 的 值
If Temp% > 0 Then '关 键 词 的 值 不 为 空
s = ""
For i = 1 To 144
If Asc(Mid$(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s & Mid$(ResultString, i, 1)
End If
Next
Else
Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, FileName)
'将 缺 省 值 写 入 INI 文 件
s = DefString
End If
GetIniS = s
End Function
'读ini数值
Public Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Integer, ByVal FileName As String) As Integer
Dim d As Long, s As String
d = DefValue
GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, FileName)
If d <> DefValue Then
s = "" & d
d = WritePrivateProfileString(SectionName, KeyWord, s, FileName)
End If
End Function
'写ini字符串
Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String, ByVal FileName As String)
Dim res%
res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, FileName)
End Sub
'写ini数值
Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Integer, ByVal FileName As String)
Dim res%, s$
s$ = str$(ValInt)
res% = WritePrivateProfileString(SectionName, KeyWord, s$, FileName)
End Sub
ByVal lpApplicationName As Long, _
ByVal lpszKey As Long, _
ByVal lpszDefault As String, _
ByVal lpszReturnBuffer As String, _
ByVal cchReturnBuffer As Long, _
ByVal lpszFile As String _
) As Long'Return Sections Name in Collection
Public Function AllSection() As Collection
On Error GoTo AllSection_Error
Dim sReturn As String
Dim iFound As Integer
Dim i As Integer
Dim iNullOffSet As Integer
Dim varAllSection As New Collection
sReturn = String$(255, 0)
Call ProfileGetAllSectionName(0, 0, "", sReturn, 255, FileFullName)
Do
iNullOffSet = InStr(sReturn, Chr$(0))
If iNullOffSet > 1 Then
varAllSection.Add Mid$(sReturn, 1, iNullOffSet - 1)
sReturn = Mid$(sReturn, iNullOffSet + 1)
End If
Loop While iNullOffSet > 1 Set AllSection = varAllSection GoTo WayOut
AllSection_Error:
WayOut:
Set varAllSection = Nothing
End Function
ByVal lpApplicationName As String, _
ByVal lpszKey As Long, _
ByVal lpszDefault As String, _
ByVal lpszReturnBuffer As String, _
ByVal cchReturnBuffer As Integer, _
ByVal lpszFile As String _
) As Long'Return Keys Name in Collection
Public Function AllKey _
(Optional ByVal isSection As String) _
As Collection
On Error GoTo AllKey_Error Dim sReturn As String
Dim iFound As Integer
Dim i As Integer
Dim iNullOffSet As Integer
Dim varAllKey As Collection
Set varAllKey = New Collection
isSection = Trim$(isSection)
If isSection = "" Then
isSection = DefaultSection
End If
sReturn = String$(255, 0)
Call ProfileGetAllKeysName(isSection, 0, "", sReturn, 255, FileFullName)
Do
iNullOffSet = InStr(sReturn, Chr$(0))
If iNullOffSet > 1 Then
varAllKey.Add Mid$(sReturn, 1, iNullOffSet - 1)
sReturn = Mid$(sReturn, iNullOffSet + 1)
End If
Loop While iNullOffSet > 1 DefaultSection = isSection
Set AllKey = varAllKey
GoTo WayOut
AllKey_Error:
WayOut:
Set varAllKey = Nothing
End Function
'注意引用microsoft scripting runtime对象
Option ExplicitDim arrSection() As String, arrkey() As String
Private Sub Command1_Click()
Dim FSO As New FileSystemObject
Dim tt As TextStream
Set tt = FSO.OpenTextFile("d:\mc\win.ini")
Dim temps As String
Dim s As String
Dim i As Long
Dim j As Long
Dim arrtemp As Variant
i = 0
Do While Not tt.AtEndOfStream
s = Trim(tt.ReadLine)
arrtemp = Split(s, "=")
j = UBound(arrtemp)
If j = 1 Then
ReDim Preserve arrSection(i)
ReDim Preserve arrkey(i)
arrSection(i) = Trim(arrtemp(0))
List1.AddItem arrSection(i)
arrkey(i) = Trim(arrtemp(1))
List2.AddItem arrkey(i)
i = i + 1
End If
Loop
Set tt = Nothing
Set FSO = Nothing
End Sub