某个ini文件中如下
[scan]
path1=d:\aa
path2=d:\bb
path3=d:\cc
....其中path.....这个键有很多个,我调用函数可以如下:
ScanPath = GetPrivateStringValue("scan", "path1", App.Path & "\my.ini")
仅仅得到了path1的值是d:\aa,想要程序自动遍历后边的path2,path3,path3......并得到后边的值
不知道如何调用函数GetPrivateStringValue可以达到.或者哪位哥哥给个例程

解决方案 »

  1.   

    我给你一个操作INI的完整模块'————————(1)————————————
    '获得指定ini文件中某个节下面的所有键值 TrueZq,,需要下面的API声明
    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
    '返回一个字符串数组
    '调用举例:
    Public arrClass() As String'获得指定ini文件中某个节下面某个子键的键值,需要下面的API声明
    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 Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPublic Function GetiniValue(ByVal lpKeyName As String, ByVal strName As String, ByVal strIniFile As String) As String
        Dim strTmp As String * 255
        
        Call GetPrivateProfileString(lpKeyName, strName, "", _
                strTmp, Len(strTmp), strIniFile)
        GetiniValue = Left$(strTmp, InStr(strTmp, vbNullChar) - 1)
    End Function    
    Public Function GetInfoSection(strSection As String, strIniFile As String) As String()
        Dim strReturn As String * 32767
        Dim strTmp As String
        Dim nStart As Integer, nEnd As Integer, i As Integer
        Dim sArray() As String
        
        Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), sys & "\oeminfo.ini")
        
        strTmp = strReturn
        i = 1
        Do While strTmp <> ""
            nStart = nEnd + 1
            nEnd = InStr(nStart, strReturn, vbNullChar)
            strTmp = Mid$(strReturn, nStart, nEnd - nStart)
            If Len(strTmp) > 0 Then
                ReDim Preserve sArray(1 To i)
                sArray(i) = strTmp
                i = i + 1
            End If
        Loop
        GetInfoSection = sArray
       
    End Function'————————(2)————————————
    '作用:去掉字符串中的首尾空格、所有无效字符
    '测试用例
    'Dim strRes As String
    'Dim strSour As String
    '
    'strSour = " " & vbNullChar & vbNullChar & " ab cd" & vbNullChar
    'strRes = zqTrim(strSour)
    Public Function zqTrim(ByVal strSour As String) As String
        Dim strTmp As String
        Dim nLen As Integer
        Dim i As Integer, j As Integer
        Dim strNow As String, strValid() As String, strNew As String
        'strNow 当前字符
        'strValid 有效字符
        'strNew 最后生成的新字符
        
        strTmp = Trim$(strSour)
        nLen = Len(strTmp)
        If nLen < 1 Then
            zqTrim = ""
            Exit Function
        End If
        j = 0
        For i = 1 To nLen
            strNow = Mid(strTmp, i, 1) '每次读取一个字符
            'MsgBox Asc(strNow)
            If strNow <> vbNullChar And Asc(strNow) <> 9 Then '如果有效,则存入有效数组
                ReDim Preserve strValid(j)
                strValid(j) = strNow
                j = j + 1
            End If
        
        Next i
        
        strNew = Join(strValid, "") '将所有有效字符连接起来
        zqTrim = Trim$(strNew) '去掉字符串中的首尾空格
    End Function'以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键
    '针对字符串值
    '空值表示出错
    Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
        On Error GoTo GetIniStrErr
        If VBA.Trim(In_Key) = "" Then
            GoTo GetIniStrErr
        End If
        Dim GetStr As String
        GetStr = VBA.String(128, 0)
        GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\~back.ini"
        GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
        If GetStr = "" Then
            GoTo GetIniStrErr
        Else
            GetIniStr = GetStr
            GetStr = ""
        End If
        Exit Function
    GetIniStrErr:
        err.Clear
        GetIniStr = ""
        GetStr = ""
    End FunctionPublic Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String, ByVal strIniFile As String) As Boolean
        On Error GoTo WriteIniStrErr
        WriteIniStr = True
        If VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
            GoTo WriteIniStrErr
        Else
            WritePrivateProfileString AppName, In_Key, In_Data, strIniFile
        End If
        Exit Function
    WriteIniStrErr:
        err.Clear
        WriteIniStr = False
    End Function
      

  2.   

    其实也可以象操作TEXT档一样,以"="为分界符,保存两边的内容.
      

  3.   

    vb6里MSDN好像找不到改API得的参数说明啊?
    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
      

  4.   

    这里的String(255, 0)
    是什么意思?谢谢
    Private 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
      

  5.   

    这里的String(255, 0)
    是什么意思?谢谢
    =====================================限定INI档中项目数据的长度,只取前面256个字符.
      

  6.   

    Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), sys & "\oeminfo.ini")
        
        strTmp = strReturn
        i = 1
        Do While strTmp <> ""
            nStart = nEnd + 1
            nEnd = InStr(nStart, strReturn, vbNullChar)
            strTmp = Mid$(strReturn, nStart, nEnd - nStart)
            If Len(strTmp) > 0 Then
                ReDim Preserve sArray(1 To i)
                sArray(i) = strTmp
                i = i + 1
            End If
        Loop
        GetInfoSection = sArray
    其中的vbNullChar没有定义就被使用,不知道什么原因啊?这个东西什么意思啊?
      

  7.   

    vbNullChar是一个VB常数,可以直接用的。就是Chr(0),值为0 的字符,这里是API返回的字符串的分隔符。
    另外,String(255, 0)的作用是预分配空间吧?