在一个配置文件中有很多段名,如下所示:
[a1]
x=1
y=2
[a2]
x=3
y=3
[a2]
x=4
y=6
[a3]
x=9
y=10
......
请教如何得到所有的段名?(得到a1,a2,a3........)

解决方案 »

  1.   

    加一个项目记录一下一共有多少个字段。
    关于INI操作如下:
    '两个函数 , 先在一个模快中定义API函数 
    Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
    '如果是读INT值可以用字符串转化,所以没有另外定义函数
    'Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPriviteProfileIntA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long'定义读与写INI文件的函数
    '****读INI文件****
    '文件名 lpFileName 如果不存在会自己创建,如果只有文件名,默认在Windows\system目录下
    '[lpAppName]
    'lpKeyName=取回的设置值
    'lpDefault 当键值不存在时的默认值
    Public Function ReadINI(lpFileName As String, lpAppName As String, LpKeyName As String) As String
    Dim Temp As String * 20
    Dim lpDefault As String
    lpDefault = ""
    If GetPrivateProfileString(lpAppName, LpKeyName, lpDefault, Temp, Len(Temp), lpFileName) <= 0 Then
        ReadINI = ""
    Else
        ReadINI = MyTrim(Temp) 'MyTrim函数见下
    End If
    End Function
    '****写INI文件****
    '[lpAppName]
    'lpKeyName=lpString
    Public Function WriteINI(lpFileName As String, lpAppName As String, LpKeyName As String, lpString As String) As Boolean
        If WritePrivateProfileString(lpAppName, LpKeyName, lpString, lpFileName) = 0 Then
            WriteINI = False
        Else
            WriteINI = True
        End If
    End Function'包含三个函数,分别取Rtrim,Ltrim,Trim
    '可以去字符串中如ASC码为0,10,13,32的字符
    Public Function MyRtrim(Tmpstr As String)
    Dim i, s As Integer
    i = Len(Tmpstr)
    If i = 0 Then
        MyRtrim = ""
        Exit Function
    End If
    s = Asc(Right(Tmpstr, 1))
    While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
        i = i - 1
        Tmpstr = Left(Tmpstr, i)
        If Len(Tmpstr) = 0 Then
            MyRtrim = ""
            Exit Function
        End If
        s = Asc(Right(Tmpstr, 1))
    Wend
    MyRtrim = Tmpstr
    End FunctionPublic Function MyLtrim(Tmpstr As String)
    Dim i, s As Integer
    i = Len(Tmpstr)
    If i = 0 Then
        MyLtrim = ""
        Exit Function
    End If
    s = Asc(Left(Tmpstr, 1))
    While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
        i = i - 1
        Tmpstr = Right(Tmpstr, i)
        If Len(Tmpstr) = 0 Then
            MyLtrim = Tmpstr
        Exit Function
        End If
        s = Asc(Left(Tmpstr, 1))
    Wend
    MyLtrim = Tmpstr
    End FunctionPublic Function MyTrim(Tmpstr As String)
    Tmpstr = MyLtrim(Tmpstr)
    Tmpstr = MyRtrim(Tmpstr)
    MyTrim = Tmpstr
    End Function
      

  2.   


        建立与读取.ini文件 
     
       虽然进入win95之後,一般读写ini文件被读写Registry所取代,但我们还是可以透过
    win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再
    读回来。目前建议使用GetSetting SaveSetting的方式存於Registry中,不用目前
    的方式。 储存程式的设定
    '请於form中放3个TextBox,一个CommandBox
    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 LongPrivate Sub Command1_Click()
    Dim success As Long
    success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini")
    '叁数一 Section Name
    '叁数二 於.ini中的项目
    '叁数三 项目的内容
    '叁数四 .ini文件的名称
    success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini")
    success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini")
    End SubPrivate Sub Form_load()
    Dim ret As Long
    Dim buff As String
    buff = String(255, 0)
    ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini")
    '若.ini MyApp中无text1,则采用叁数三的值
    Text1.Text = buff
    buff = String(255, 0)
    ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
    Text2.Text = buff
    buff = String(255, 0)
    ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
    Text3.Text = buff
    End Sub
      
     
       
     
      
     
      

  3.   


    'require variable declaration
    Option Explicit'declares for ini controlling
    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    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 WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, 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'when form is loaded
    Private Sub Form_Load()'if error occures resume still
    On Error Resume Next'local variables
    Dim File As String, OFLen As Double, _
        Str As String'set our varibles
    File = "C:\temp.txt"
    OFLen = FileLen(File)'write few example sections:
    WriteIniSection File, "Test1", ""
    WriteIniSection File, "Test2", "Here shoud be found some text"'write few ini strings
    WriteIni File, "Test3", "Ini1", "This is ini 1"
    WriteIni File, "Test1", "Ini2", "This is ini 2"'inform we're written the data
    MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)'read the ini file
    Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
    Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
    Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
    Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf'show data
    MsgBox Str'end application
    EndEnd Sub'// INI CONTROLLING PROCEDURES'reads ini string
    Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    Dim RetVal As String * 255, v As Long
    v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
    ReadIni = Left(RetVal, v - 1)
    End Function'reads ini section
    Public Function ReadIniSection(Filename As String, Section As String) As String
    Dim RetVal As String * 255, v As Long
    v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    ReadIniSection = Left(RetVal, v - 1)
    End Function'writes ini
    Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    WritePrivateProfileString Section, Key, Value, Filename
    End Sub'writes ini section
    Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
    WritePrivateProfileSection Section, Value, Filename
    End Sub
      

  4.   

    '模块说明:用于对INI文件的读写操作
    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'功能简介:对INI文件进行读操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
        GetProfileString = String(255, 0)
        GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
        GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
    End Function'功能简介:对INI文件进行写操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    '参数四:写操作字符串
    Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
        On Error GoTo WriteErr
        WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
        WriteProfilestring = True
        Exit Function
    WriteErr:
    End Function
      

  5.   


    '* *
    '* *    Section Operation
    '* *'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