'cFileCFG.cls
Dim StrArr() As String
Public Function Value_Read(Key As String) As String
Dim strTemp As String
Dim lngSet As Long
For I = LBound(StrArr) To UBound(StrArr)
lngSet = InStr(StrArr(I), " ")
If lngSet > 0 Then
strTemp = Left$(StrArr(I), lngSet - 1)
If LCase$(strTemp) = Key Then
Value_Read = Mid$(StrArr(I), lngSet + 1)
End If
End If
Next
End Function
Public Sub Value_Write(Value As String, Key As String)
Dim strTemp As String
Dim lngSet As Long
For I = LBound(StrArr) To UBound(StrArr)
lngSet = InStr(StrArr(I), " ")
If lngSet > 0 Then
strTemp = Left$(StrArr(I), lngSet - 1)
If LCase$(strTemp) = Key Then
StrArr(I) = strTemp & " " & Value
End If
End If
Next
End Sub
Public Function LoadFile(FileName As String) As String()
Dim FileID As Long
Dim ID As Long
FileID = FreeFile
Open FileName For Input As #FileID
Do While Not EOF(FileID)
LineStr = ""
ReDim Preserve StrArr(ID)
Line Input #FileID, StrArr(ID)
ID = ID + 1
Loop
Close #FileID
LoadFile = StrArr
End Function
Public Sub SaveFile(FileName As String)
Dim FileID As Long
Dim I As Long
FileID = FreeFile
Open FileName For Output As #FileID
For I = LBound(StrArr) To UBound(StrArr)
Print #FileID, StrArr(I)
Next
Close #FileID
End Sub
调用实例
Private Sub Command1_Click()
Dim str() As String
Dim cfg As New cFileCFG
str = cfg.LoadFile("F:/00_GAME_00/Counter-Strike/cstrike/config.cfg")
MsgBox cfg.Value_Read("name")
cfg.Value_Write "youname", "name"
cfg.SaveFile "F:/00_GAME_00/Counter-Strike/cstrike/config.cfg"
End Sub