当前位置:编程学习 > VB >>

VB保存加载配置ini

有check和option的value

还有text

combo(list)里的数据

答案:'其实INI操作有专门的API,不过我之前已经动手写了自读INI的。以下代码不会识别分号注释符。使用方法:

'ReadINI("a","b","c")返回[a]中b的值,如果找不到返回"c".

'WriteINI("a","b","c")将[a]中b的值设为"c".

'ReadINIs()其实就是ReadINI多个合在一起操作,只是加快速度。

'WriteINIs()类似。

'另外,现在我已经不用INI保存配置了,XML技术更先进,运行效率更高,数据结构更灵活,相关COM组件"Microsoft XML,v???"每台电脑都会有,推荐使用。

Public Function ReadINI(Section As String, Key As String, Optional Default As String = "") As String
    Dim N As TextStream, fso As New FileSystemObject, s As String, step As Integer
    Set N = fso.OpenTextFile(App.Path & "\配置设置.ini")
    While Not N.AtEndOfStream
        s = N.ReadLine
        If step = 1 And LCase(Left(s, Len(Key) + 1)) = LCase(Key) & "=" Then
            ReadINI = mid(s, Len(Key) + 2)
            If ReadINI = "" Then ReadINI = Default
            N.Close
            Exit Function
        End If
        If s = "[" & Section & "]" And step = 0 Then
            step = 1
        End If
    Wend
    N.Close
    ReadINI = Default
Exit Function
EndIt:
    ReadINI = Default
    Msgbox "加载设置时发生错误[" & CStr(Err.Number) & "]:" & Err.Description, vbOKOnly + vbCritical, "文件错误"
End Function
Public Function ReadINIs(Sections() As String, Keys() As String) As String()
    On Error GoTo EndIt
    Dim steps() As Byte, i As Integer, b As Boolean
    Dim Result() As String, s As String
    ReDim steps(LBound(Keys) To UBound(Keys))
    ReDim Result(LBound(Keys) To UBound(Keys))
    Dim N As TextStream, fso As New FileSystemObject
    Set N = fso.OpenTextFile(App.Path & "\配置设置.ini")
    b = UBound(Sections) = LBound(Sections)
    While Not N.AtEndOfStream
        s = N.ReadLine
        For i = LBound(Keys) To UBound(Keys)
            If steps(i) = 1 And LCase(Left(s, Len(Keys(i)) + 1)) = LCase(Keys(i)) & "=" Then
                Result(i) = mid(s, Len(Keys(i)) + 2)
                steps(i) = 2
            End If
            If steps(i) = 1 And Left(s, 1) = "[" And Right(s, 1) = "]" Then
                steps(i) = 2
            End If
            If b Then
                If s = "[" & Sections(LBound(Sections)) & "]" And steps(i) = 0 Then
                    steps(i) = 1
                End If
            Else
                If s = "[" & Sections(i) & "]" And steps(i) = 0 Then
                    steps(i) = 1
                End If
            End If
        Next
    Wend
    N.Close
    ReadINIs = Result
    Exit Function
EndIt:
    Msgbox "加载设置时发生错误[" & CStr(Err.Number) & "]:" & Err.Description, vbOKOnly + vbCritical, "文件错误"
    ReadINIs = Keys
End Function

Public Sub WriteINIs(Sections() As String, Keys() As String, Settings() As String)
    On Error GoTo EndIt
    Dim steps() As Byte, i As Integer, j As Integer
    Dim s As String, d As String
    ReDim steps(LBound(Keys) To UBound(Keys))
    For i = LBound(Keys) To UBound(Keys) - 1
        For j = i To UBound(Keys)
            If StrComp(Sections(i), Sections(j)) > 0 Then
                s = Sections(j)
                Sections(j) = Sections(i)
                Sections(i) = s
                s = Keys(j)
                Keys(j) = Keys(i)
                Keys(i) = s
                s = Settings(j)
                Settings(j) = Settings(i)
                Settings(i) = s
            End If
        Next
    Next
    Dim N As TextStream, fso As New FileSystemObject
    Set N = fso.OpenTextFile(App.Path & "\配置设置.ini")
    While Not N.AtEndOfStream
        s = N.ReadLine
        For i = LBound(Keys) To UBound(Keys)
            If steps(i) = 1 And LCase(Left(s, Len(Keys(i)) + 1)) = LCase(Keys(i)) & "=" Then
                s = Keys(i) & "=" & Settings(i)
                steps(i) = 2
            End If
            If steps(i) = 1 And Left(s, 1) = "[" And Right(s, 1) = "]" Then
                s = Keys(i) & "=" & Settings(i) & vbCrLf & s
            End If
            If s = "[" & Sections(i) & "]" And steps(i) = 0 Then
      &n

上一个:计算机vB中的二叉树
下一个:用vb做一个计算器

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,