怎么循环的读取ini里面1到100的数字。当手动添加101或者到110或者更多。怎么用循环读取!
用For k = 1 To 100  只能读取已知数据。不能读取手动添加的数据

解决方案 »

  1.   

    非文件尾部继续读
    while not eof(1)
    ...
    ...
    wend
      

  2.   

    Option ExplicitPrivate Declare Function GetPrivateProfileSection Lib "KERNEL32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic Function GetInfoSection(strSection As String, strIniFile As String) As String()
              Dim strReturn     As String * 32767
              Dim strTmp     As String
              Dim nStart     As Integer
              Dim nEnd       As Integer
              Dim i       As Integer
              Dim sArray()     As String
                
              Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), strIniFile)
                
              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 FunctionPrivate Sub Command1_Click()
      Dim i As Long
      Dim A() As String
      A = GetInfoSection("aaa", "c:\1.ini") 'C:\1.ini 改为你的文件名
      For i = 1 To UBound(A)
        MsgBox A(i)
      Next
    End Sub参考:
    http://topic.csdn.net/u/20080910/18/99A5A17A-1AB7-41F3-A866-855F4F1EA2D7.html
      

  3.   

    '此代码由“正则测试工具 v1.1.32”自动生成,请直接调用TestReg过程
    Private Sub TestReg()
        Dim strData As String
        Dim reg As Object
        Dim matchs As Object, match As Object    strData = "[Windows 帮助]"  &  vbCrLf  & _
                  "H_WindowPosition=[426,266,426,266,0]"  &  vbCrLf  & _
                  "[SysParam]"  &  vbCrLf  & _
                  "RemPass=1"  &  vbCrLf  & _
                  "ServerCount=2"  &  vbCrLf  & _
                  "server0=127.0.0.1"  &  vbCrLf  & _
                  "UserCount=1"    Set reg = CreateObject("vbscript.regExp")
        reg.Global = True
        reg.IgnoreCase = True
        reg.MultiLine = True
        reg.Pattern = "^\s*\[(.*)\]\s*$"
        Set matchs = reg.Execute(strData)
        For Each match In matchs
            Debug.Print match.SubMatches(0)
        Next
    End Sub
    ===============================
    Windows 帮助
    SysParam
      

  4.   

    本帖最后由 bcrun 于 2011-03-05 13:05:49 编辑
      

  5.   

    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 GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic Function ReadIniFile(ByVal strSection As String, ByVal strKey As String, _
      Optional ByVal blnStoreDefaultValue As Boolean = False, _
      Optional ByVal strKeyValue As String = "") As String
        
      Dim strReadBuffer As String * 1024
      Dim lngReturn As Long
        
      lngReturn = GetPrivateProfileString(strSection, strKey, "", strReadBuffer, Len(strReadBuffer), gStation.IniFile)
      If lngReturn = 0 And blnStoreDefaultValue = True Then
      WriteIniFile strSection, strKey, strKeyValue
      lngReturn = GetPrivateProfileString(strSection, strKey, "", strReadBuffer, Len(strReadBuffer), gStation.IniFile)
      End If
      ReadIniFile = Left(strReadBuffer, lngReturn)
    End Function
      

  6.   

    http://download.csdn.net/source/3057337