有一个InI文件,需要读取,section数目不确定,格式如下,在程序中需要读取该ini,在section不取定的情况下,读取变量的个数难以控制,比如说现在有三个section 那我就要写18个变量,如果section有100个,那该如何处理?请各位指点!
[UpdateFileInfo1]
FileName=
Version=
FileDate=
DownLoadPath1=
DownLoadPath2=
DownLoadPath3=[UpdateFileInfo2]
FileName=
Version=
FileDate=
DownLoadPath1=
DownLoadPath2=
DownLoadPath3=[UpdateFileInfo3]
FileName=
Version=
FileDate=
DownLoadPath1=
DownLoadPath2=
DownLoadPath3=
............

解决方案 »

  1.   

    使用数组呗!给你两段例子:
    '*****************************************************
    '   读取*.ini文件中的所有项目
    '=====================================================
    '   lpFilename - *.ini文件名
    '   SectionArry() - 存储返回的项目
    '=====================================================
    '   函数返回数组的最大下标值。(数组最小下标值为0)
    '   若没有找到任何值则函数返回-1
    '*****************************************************
    Public Function GetPrivateProfileAllSection(SectionArry() As String, lpFileName As String) As Long
        Dim s As String
        Dim i, Max As Integer
        
        s = Space(1024)
        GetPrivateProfileString 0&, 0&, "", s, 1024, lpFileName
        
        SectionArry = Split(s, Chr(0))
        Max = UBound(SectionArry) - 2
        If Max >= 0 Then
            ReDim Preserve SectionArry(Max)
        End If
        GetPrivateProfileAllSection = Max
    End Function'***********************************************************************************
    '   读取*.ini文件中指定小节下的所有关键字和值,每个关键字和值在数组中的位置一一对应
    '===================================================================================
    '   pFile - *.ini文件名
    '   KeyString - 小节名
    '   KeyArry() - 存储返回的所有关键字
    '   ValueArry() - 存储返回的所有键值
    '===================================================================================
    '   函数返回关键字和键值数组的最大下标值。(数组最小下标值为0)
    '***********************************************************************************
    Public Function GetPrivateProfileSectionKeyValue(SectionName As String, pFile As String, KeyArry() As String, ValueArry() As String) As Long
        Dim TempStr As String
        Dim i, j, Max As Integer
        Dim Rcode As Integer
        Dim StringArry() As String
        Dim l As Long
        
        TempStr = Space(32676)    l = GetPrivateProfileSection(SectionName, TempStr, 32676, pFile)
        
        If l <> 0 Then
            TempStr = RTrim$(TempStr)
            TempStr = Replace(TempStr, "=", Chr(0))
            StringArry = Split(TempStr, Chr(0))
            Max = UBound(StringArry) - 2
            Rcode = (Max - 1) / 2
            
            ReDim KeyArry(Rcode)
            ReDim ValueArry(Rcode)
            j = 0
            For i = 0 To Rcode
                KeyArry(i) = StringArry(j)
                j = j + 1
                ValueArry(i) = StringArry(j)
                j = j + 1
            Next
            Erase StringArry
        Else
            Rcode = -1
        End If
        GetPrivateProfileSectionKeyValue = Rcode
    End Function
      

  2.   

    你加一个
    [System]
    SessionNum=100
    不就什么问题都没有了,真笨
      

  3.   

    我如何把KeyArry(), ValueArry() 中的值一一取出?我才学vb,请指教!
      

  4.   

    For i = 0 To (GetPrivateProfileAllSection(SectionArry(), App.Path + "\" + "config.ini"))
    '        Debug.Print SectionArry(i)
        For j = 0 To GetPrivateProfileSectionKeyValue(SectionArry(i), App.Path + "\" + "config.ini", KeyArry(), ValueArry())
    '        Debug.Print KeyArry(j) + "=" + ValueArry(j)
    这里怎么用变量实现取值?
        Next
    Next
      

  5.   

    我的网站上有操作INI文件的源码,你可以看看。VB资料->查询“操作INI文件”;==========================
    免费的学习、交流、源码、工具下载网站,欢迎大家访问!
    http://www.j2soft.cn/