我的ini文件类似如下
[PELOD-1]
UP=    \FF\20\3A\F4\B5\6C\0C
DOWN=  \09\08\07\06\05\04\03
[PELOD-2]
UP=    \FF\20\3A\F4\B5\6C\0F
DOWN=  \09\08\07\06\05\04\03
我用api函数访问该文件
软件启动时我要把所有的段名称(PELOD-1  PELOD-2)都插入到Combo控件里
请问用那个函数取出阻断名称阿

解决方案 »

  1.   

    Function GetProfile(strFileName As String, strSection As String, strName As String) As String
      '这个函数是用来对INI文件进行读操作的
      '函数说明:
      'strFileName 是所要读取的文件名
      'strSection  是这个文件中的一个节点名
      'strName 是所要查找的字段名
      '返回值:
       strSectionTemp = ""
       strNameTemp = ""
       strreturn = ""
       On Error GoTo ErrSrchSection
       Open strFileName For Input As #1
       ' 下面这段程序是用来查找节点的
         Do While Not EOF(1)
            strCharA = Input(1, #1)
            If strCharA = "[" Then
               Do While Not EOF(1)
                 strCharB = Input(1, #1)
                 If strCharB = "]" Then Exit Do
                 strSectionTemp = strSectionTemp & strCharB
               Loop
            End If
            If strSectionTemp = strSection Then
              strCharA = Input(2, #1)
              Exit Do
            Else
              strSectionTemp = ""
            End If
         Loop
     On Error GoTo ErrReadFile
      
    aa:
        '下面这段程序是用来查找所要查找的字段的
        strNameTemp = ""
        Do While Not EOF(1)
          strCharA = Input(1, #1)
          If strCharA <> "=" Then
            strNameTemp = strNameTemp & strCharA  '得到名称
          Else
            Exit Do
          End If
        Loop
            If strNameTemp = strName Then
           Line Input #1, strreturn  '如果找到与它匹配的字段名,就返回得到的值
        Else
           Line Input #1, strreturn  '如果未找到与它匹配的字段名,就继续找
           GoTo aa
        End If
        Close #1
        GetProfile = strreturn
        Exit Function
    ErrReadFile:
        Dim inrRet As Integer
        intret = MsgBox("在文件中没有找到所要查找的字段", vbAbortRetryIgnore, "错误信息")
        Select Case intret
           Case vbAbort
              GetProfile = ""
              Close #1
              Exit Function
           Case vbRetry
              Resume
           Case vbIgnore
              Resume Next
         End Select
    ErrSrchSection:
         MsgBox "节点未找到", vbOKOnly
         GetProfile = ""
         Close #1
    End Function
      

  2.   

    老兄给我的代码对我又帮助,帮人就帮到底吧,我做的程序不但涉及到了 读ini,还涉及到了写、删除ini文件里的内容,还请多多指教。我一定实现我承诺的分数。
      

  3.   

    给你一个类模块Option Explicit'****************************************************************************
    '* Class:
    '*    INISettings
    '*
    '* Description:
    '*    INI file settings class
    '****************************************************************************
    Private Const CodeVersion As String = "v1.01.00"' Class member variables
    Private m_strINIFileName As String' Module-level variables' INI setting WIn32 APIs
    Private Declare Function GetPrivateProfileInt _
      Lib "kernel32" _
      Alias "GetPrivateProfileIntA" ( _
      ByVal SectionName As String, _
      ByVal KeyName As String, _
      ByVal Default As Long, _
      ByVal FileName As String) _
    As LongPrivate Declare Function GetPrivateProfileString _
      Lib "kernel32" _
      Alias "GetPrivateProfileStringA" ( _
      ByVal SectionName As String, _
      ByVal KeyName As String, _
      ByVal Default As String, _
      ByVal ReturnedString As String, _
      ByVal StringSize As Long, _
      ByVal FileName As String) _
    As LongPrivate Declare Function WritePrivateProfileString _
      Lib "kernel32" _
      Alias "WritePrivateProfileStringA" ( _
      ByVal SectionName As String, _
      ByVal KeyName As String, _
      ByVal KeyValue As String, _
      ByVal FileName As String) _
    As Long' There is no WritePrivateProfileInt declaration...Private Declare Function DeleteKeyValue _
      Lib "kernel32" _
      Alias "WritePrivateProfileStringA" ( _
      ByVal SectionName As String, _
      ByVal KeyName As String, _
      ByVal KeyValue As Long, _
      ByVal FileName As String) _
    As LongPrivate Declare Function GetINIKeys _
      Lib "kernel32" _
      Alias "GetPrivateProfileStringA" ( _
      ByVal SetionName As String, _
      ByVal KeyName As Long, _
      ByVal Default As String, _
      ByVal ReturnedString As String, _
      ByVal StringSize As Long, _
      ByVal FileName As String) _
    As LongPrivate Declare Function GetINISections _
      Lib "kernel32" _
      Alias "GetPrivateProfileStringA" ( _
      ByVal SetionName As Long, _
      ByVal KeyName As Long, _
      ByVal Default As Long, _
      ByVal ReturnedString As String, _
      ByVal StringSize As Long, _
      ByVal FileName As String) _
    As LongPrivate Sub Class_Initialize()
      ' Set initial values to defaults which may be overridden
      ' with property settings
      '
      m_strINIFileName = IIf(Left$(App.Path, 1) = "\", App.Path, App.Path & "\") & _
                    App.EXEName & ".ini"
    End Sub
    Public Property Get INIFileName() As String
      ' Returns: the value of the INIFileName property
      '
      INIFileName = m_strINIFileName
    End Property
            
    Public Property Let INIFileName(ByVal INISettingsFile As String)
      ' INISettingsFile - FullPath to INI file, if this is empty used app.path and
      '                   App.ExeName bydefault
      '
      INISettingsFile = Trim$(INISettingsFile)
      
      If INISettingsFile = "" Then
        m_strINIFileName = IIf(Left$(App.Path, 1) = "\", App.Path, App.Path & "\") & _
                      App.EXEName & ".ini"
      Else
        If InStr(INISettingsFile, "\") > 0 Then
          ' Entire path and file passed
          m_strINIFileName = Trim$(INISettingsFile)
        Else
          ' Assume only filename passed
        m_strINIFileName = IIf(Left$(App.Path, 1) = "\", App.Path, App.Path & "\") & _
                      Trim$(INISettingsFile)
        End If
      End If
    End Property
    ' Public MethodsPublic Sub DeleteKey(ByVal Section As String, ByVal KeyName As String)
      ' Deletes the specified key. Deleting with empty KeyName deletes the Section.
      Dim iRet As Integer
      
      iRet = DeleteKeyValue(Section, KeyName, 0&, INIFileName)
    End Sub
    Public Function GetSettingAll(ByVal Section As String) As Variant
      ' Returns all settings in a given Section in a 2-dim String array
      ' Dim sArray(,) As String
      Dim sArray() As Variant
      Dim sKeys() As String
      Dim iRet As Long, iCnt As Long
      Dim iKeys As Long, iKey As Long
      Dim sBuf As String
      sBuf = Space$(2048)
      
      iRet = GetINIKeys(Section, 0&, 0&, sBuf, Len(sBuf), INIFileName)
      If (iRet > 1) Then              'if any returned
        iCnt = InStr(sBuf, vbNullChar & vbNullChar)     'find end of list
        sKeys = Split(Left$(sBuf, iCnt - 1), vbNullChar) 'separate keys
        iKeys = UBound(sKeys)       'get count
        ReDim sArray(iKeys, 1)      'make string array
        For iKey = 0 To iKeys       'fill array
          sArray(iKey, 0) = sKeys(iKey)
          sArray(iKey, 1) = GetSettingStr(Section, sKeys(iKey), "")
        Next
        GetSettingAll = sArray
      End If
    End FunctionPublic Function GetSectionNames() As String()
      'Returns all Section names as a string array
      Dim sArray() As String
      Dim sKeys() As String
      Dim iRet As Long, iCnt As Long
      Dim iKeys As Long, iKey As Long
      Dim sBuf As String
      sBuf = Space$(2048)  iRet = GetINISections(0&, 0&, 0&, sBuf, Len(sBuf), INIFileName)
      If (iRet > 1) Then              'if any returned
        iCnt = InStr(sBuf, vbNullChar & vbNullChar)     'find end of list
        sKeys = Split(Left$(sBuf, iCnt - 1), vbNullChar) 'separate keys
        iKeys = UBound(sKeys)       'get count
        ReDim sArray(iKeys)
        For iKey = 0 To iKeys       'fill array
          sArray(iKey) = sKeys(iKey)
        Next
        GetSectionNames = sArray
      End If
    End FunctionPublic Function GetSettingStr(ByVal Section As String, ByVal KeyName As String, ByVal DefaultValue As String) As String
      'returns a string setting
      Dim iRet As Integer
      Dim sBuf As String
      
      sBuf = Space$(128)
      iRet = GetPrivateProfileString(Section, KeyName, DefaultValue, sBuf, Len(sBuf), INIFileName)
      'Return sBuf.Substring(0, iRet)
      GetSettingStr = Left(sBuf, iRet)
    End FunctionPublic Function GetSettingInt(ByVal Section As String, ByVal KeyName As String, ByVal DefaultValue As Integer) As Integer
      ' Returns a numeric setting
      Dim sSetting As String
      GetSettingInt = GetPrivateProfileInt(Section, KeyName, DefaultValue, INIFileName)
    End FunctionPublic Sub SaveSettingStr(ByVal Section As String, ByVal KeyName As String, ByVal Setting As String)
      ' Saves a string setting
      Dim iRet As Integer
      iRet = WritePrivateProfileString(Section, KeyName, Setting, INIFileName)
    End SubPublic Sub SaveSettingInt(ByVal Section As String, ByVal KeyName As String, ByRef Setting As Integer)
      ' Saves a numeric setting
      Dim iRet As Integer
      
      iRet = WritePrivateProfileString(Section, KeyName, CStr(Setting), INIFileName)
    End Sub