回 帖 发 新 帖 刷新版面

主题: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

本人业余电脑爱好者,请各位老师们指教!

回复列表 (共4个回复)

沙发

何必这么麻烦,直接用读写ini文件的api函数就可以了。
[code=c]
Option Explicit

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 Long
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function GetValue(ByVal strIniFile As String, ByVal strSec As String, ByVal strItem As String, Optional ByVal strDef As String = "defaultValue") As String

    On Error GoTo errHandler

    Dim lRet    As Long
    Dim strTemp As String
    
    strTemp = String$(254, Chr$(0))
    
    lRet = GetPrivateProfileString(strSec, strItem, strDef, strTemp, 254, strIniFile)
    
    GetValue = Trim$(Left$(strTemp, lRet))
    
    If GetValue = "" Then GetValue = strDef
    
    Exit Function

errHandler:
    Debug.Print Err.Number, Err.Description
    GetValue = strDef
End Function

Public Function SetValue(ByVal strIniFile As String, ByVal strSec As String, ByVal strItem As String, ByVal strValue As String) As Long

    On Error Resume Next

    Dim lRet As Long
    
    lRet = WritePrivateProfileString(strSec, strItem, strValue, strIniFile)
    
    SetValue = IIf(lRet = 0, -1, 0)
End Function

'''''''''''''''''''''''''
'' 读取值 strRet=getValue("ini文件路径","节点名称","项名称","默认值")
'' 保存值 setValue("ini文件路径","节点名称","项名称","值")

[/code]

板凳


[url=http://www.bootboots.com]ugg boots[/url]
[url=http://www.salelouboutin.com]christian louboutin[/url]
[url=http://www.buylouboutin.com]louboutin[/url]
[url=http://www.bootboots.com]cheap ugg boots[/url]
[url=http://www.bootboots.com]discount ugg boots[/url]
[url=http://www.bootboots.com]ugg boots sale[/url]
[url=http://www.bestlouisvuitton.com]lv handbags[/url]
[url=http://www.sale-mbt.com]mbt shoes[/url]
[url=http://www.discount-christianlouboutin.com]christian louboutin[/url]

3 楼



Everybody must be know some famous brands of the world,just like the [url=http://www.wto-sell.com]Handbags[/url]: [url=http://www.sell-brand-bag.com]Hermes handbags[/url], [url=http://www.wto-sell.com]Chanel Handbags[/url],shoes about [url=http://www.2010christianlouboutin.com]Christian Louboutin[/url], [url=http://www.wto-store.com]Christian Louboutin[/url].Every brand will make you more charming.But do you think of that some day you can get it to your own,that's it,here has the proof that this is the truth.You will be shock by it.

4 楼

067
[url=http://www.enjoy-watches.com/]Watches[/url]
[url=http://www.enjoy-watches.com/]rolex[/url]
[url=http://www.enjoy-watches.com/]rolex watches[/url]
[url=http://www.enjoy-watches.com/]Replica Watches[/url]
[url=http://www.enjoy-watches.com/]Breitling Replica[/url]
[url=http://www.enjoy-watches.com/]Replica Rolex[/url]
[url=http://www.enjoy-watches.com/]Rolex Replica[/url]
[url=http://www.rolexsaleshop.com/]Rolex[/url]
[url=http://www.rolexsaleshop.com/]Replica Rolex[/url]
[url=http://www.rolexsaleshop.com/]Rolex Watches[/url]
[url=http://www.rolexsaleshop.com/]Replica Watches[/url]
[url=http://www.rolexsaleshop.com/]Breitling[/url]
[url=http://www.rolexsaleshop.com/]breitling watches[/url]
[url=http://www.rolexsaleshop.com/]omega watches[/url]
[url=http://www.rolexsaleshop.com/]cartier watches[/url]
[url=http://www.rolexsaleshop.com/]chanel watches[/url]
[url=http://www.zipposale.com/]zippo lighters[/url]
[url=http://www.zipposale.com/]zippo[/url]

我来回复

您尚未登录,请登录后再回复。点此登录或注册