主题:vb 实现参数文件的读写
Option Explicit
Public Const filename1 As String = "d:\clockset.txt"
Public mycolor As Long
_________________________________________________________
......
mycolor = Form1.Label1.ForeColor
If Dir(filename1$) = "" Then '判断文件是否存在,如不存在直接写入新文件
Open filename1$ For Append As #1
Print #1, "mycolor="; "&H" & CStr(Hex(mycolor)) & "&"
Close #1
Else
colordate = "&H" & CStr(Hex(mycolor)) & "&"
Module1.checkdate "mycolor", colordate '文件存在,调用过程改写文件
End If
......
____________________________________________________________
Public Sub checkdate(ByVal a1 As String, ByVal a2) '此过程可以读写多行的配置文件
Dim allstr As String
Dim str1 As String
Dim position2 As Long
Dim lenght As Long
Dim str2 As String
Dim datechecked As Boolean
datechecked = False
Open filename1$ For Input As #1
Do While Not EOF(1)
Line Input #1, str1 '读入一行
If Len(str1) > 5 And Trim(str1) <> "" Then '判断是否空行
position2 = InStr(str1$, "=") '判断"="号位置
lenght = position2 - 1 '判断"="前面变量长度
str2$ = Left$(str1$, lenght) '获取变量名
If a1$ = str2$ Then
allstr = allstr + str2 + "=" + CStr(a2) + vbCrLf '如文件中存在要改写的变量名则改写
datechecked = True
ElseIf a1$ <> str2$ Then
allstr = allstr + str1 + vbCrLf
End If
End If
Loop
If datechecked = False Then
allstr = allstr + a1 + "=" + CStr(a2) + vbCrLf '如文件中没有要改写的变量则在文件尾写入一行
End If
Close #1
Open filename1$ For Output As #1
Print #1, allstr '将参数写入文件
Close #1
End Sub
以上为本人实现的参数文件写入的实现代码,读取部份的如下:
Sub readdate()
Dim dateposition As Long
Dim datename As String
Dim datelenght As Long
Dim date02 As String
If Dir(filename1$) <> "" Then
Open filename1$ For Input As #1
Do While Not EOF(1)
Line Input #1, date02$ '读取一行
If Len(date02) > 5 And Trim(date02) <> "" Then '跳过空行
dateposition = InStr(date02, "=")
datelenght = dateposition - 1
datename$ = Left$(date02, datelenght)
Select Case datename$ '不同的变量实现不同的功能
Case "mycolor"
Form1.Label1.ForeColor = Val(Trim$(Right$(date02, Len(date02$) - dateposition)))
......
end sub
本人业余电脑爱好者,请各位老师们指教!
Public Const filename1 As String = "d:\clockset.txt"
Public mycolor As Long
_________________________________________________________
......
mycolor = Form1.Label1.ForeColor
If Dir(filename1$) = "" Then '判断文件是否存在,如不存在直接写入新文件
Open filename1$ For Append As #1
Print #1, "mycolor="; "&H" & CStr(Hex(mycolor)) & "&"
Close #1
Else
colordate = "&H" & CStr(Hex(mycolor)) & "&"
Module1.checkdate "mycolor", colordate '文件存在,调用过程改写文件
End If
......
____________________________________________________________
Public Sub checkdate(ByVal a1 As String, ByVal a2) '此过程可以读写多行的配置文件
Dim allstr As String
Dim str1 As String
Dim position2 As Long
Dim lenght As Long
Dim str2 As String
Dim datechecked As Boolean
datechecked = False
Open filename1$ For Input As #1
Do While Not EOF(1)
Line Input #1, str1 '读入一行
If Len(str1) > 5 And Trim(str1) <> "" Then '判断是否空行
position2 = InStr(str1$, "=") '判断"="号位置
lenght = position2 - 1 '判断"="前面变量长度
str2$ = Left$(str1$, lenght) '获取变量名
If a1$ = str2$ Then
allstr = allstr + str2 + "=" + CStr(a2) + vbCrLf '如文件中存在要改写的变量名则改写
datechecked = True
ElseIf a1$ <> str2$ Then
allstr = allstr + str1 + vbCrLf
End If
End If
Loop
If datechecked = False Then
allstr = allstr + a1 + "=" + CStr(a2) + vbCrLf '如文件中没有要改写的变量则在文件尾写入一行
End If
Close #1
Open filename1$ For Output As #1
Print #1, allstr '将参数写入文件
Close #1
End Sub
以上为本人实现的参数文件写入的实现代码,读取部份的如下:
Sub readdate()
Dim dateposition As Long
Dim datename As String
Dim datelenght As Long
Dim date02 As String
If Dir(filename1$) <> "" Then
Open filename1$ For Input As #1
Do While Not EOF(1)
Line Input #1, date02$ '读取一行
If Len(date02) > 5 And Trim(date02) <> "" Then '跳过空行
dateposition = InStr(date02, "=")
datelenght = dateposition - 1
datename$ = Left$(date02, datelenght)
Select Case datename$ '不同的变量实现不同的功能
Case "mycolor"
Form1.Label1.ForeColor = Val(Trim$(Right$(date02, Len(date02$) - dateposition)))
......
end sub
本人业余电脑爱好者,请各位老师们指教!