我查看了下配置文件
开头为【】形式的,称Section,现在不知道怎么生成ini格式的文件,并命名,并且怎么初始话里面的段这个信息,是手动创建的吗?
读写文件我大概知道用下面的API函数
GetPrivateProfileInt
     GetPrivateProfileString
     WritePrivateProfileString

解决方案 »

  1.   

    [section1]
    keyword1=valuel
    keyword2=value2
    ……
    [section2]
    keyword1=value1
    keyword2=value2
    ……可以手动创建一个初始ini文件,再在程序里面读取或修改。
    网上搜索示例一大把
      

  2.   

    就像创建普通文件,用ini做结尾。
      

  3.   

    通常都是手动修改ini文件,代码中读取参数来达到对程序的控制
      

  4.   

    提供一种简单思路
    生成:
    open "name.ini"for output as #1
    print #1,"【】1111:" & 设置1的值
    print #1,"【】2222:" & 设置2的值
    ....
    ....
    close #1
    读取:
    dim m$
    open "name.ini" for input as #2
    do while not eof(1)
    line input #!,m
    if instr(m,"【】1111:")=1 then 设置1=right(m,len(m)-7)
    if instr(m,"【】2222:")=1 then 设置2=right(m,len(m)-7)
    ....
    ....
    close #2
      

  5.   

    读ini
    s = Space(256)
    GetPrivateProfileString "小节名", _
         "关键字", "", s, 1024, "配置文件绝对路径"
    写ini
    WritePrivateProfileString "小节名", "关键字", "关键字的值", "配置文件绝对路径"
      

  6.   

    楼主可使用在这个帖中我在#16楼给出的改良版代码,当然,也可使用另外几人的:)http://topic.csdn.net/u/20100225/22/fe60c38f-a786-40de-815a-d55ffab404a0.html
    急...在线等!ini文件如何在不知道有多少个key的情况下,读取某小节中的全部内容.
      

  7.   

    Option ExplicitPrivate Const NULL_KEY_VALUE = "NOT EXIST"Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
        "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal _
        lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As BooleanPrivate 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
        
    Private Declare Function GetPrivateProfileSectionNames _
      Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" _
      (ByVal lpReturnBuffer As String, ByVal nSize As Long, _
      ByVal lpName 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 lpName As String) As Long
      
    Private sIniPath As String
    Private sIniFile As String
    Private AppPath As String
    Private Sub Class_Initialize()
        Dim iCount As Integer
        Dim sKeyValue As String * 255
        Dim sTmpStr As String
        
        If InStr(UCase$(App.Path), "PROJECTS") = 0 Then
            AppPath = App.Path
        Else 'to prevent error when running in development situation
            AppPath = Left$(App.Path, Len(App.Path) - 9)
        End If
        
        sIniFile = "\PCPOS.INI"
        
        If Dir(AppPath & sIniFile) = "" Then
    '       if local .ini file not found, create it
            WritePrivateProfileString "General", "IniFilePath", "Current", AppPath & sIniFile
            WritePrivateProfileString "General", "DataPath", AppPath & "\DATA", AppPath & sIniFile
            WritePrivateProfileString "Initialize", "bModemCable", "True", AppPath & sIniFile
    '       WritePrivateProfileString "Terminal", "SalesDataPath", AppPath & "\DATA", AppPath & sIniFile
    '       WritePrivateProfileString "Terminal", "Terminal", "0", AppPath & sIniFile
            sIniPath = AppPath
            Exit Sub
        End If
        
        iCount = GetPrivateProfileString("General", "IniFilePath", NULL_KEY_VALUE, sKeyValue, Len(sKeyValue), AppPath & sIniFile)
        If iCount = 0 Then
            Err.Raise vbObjectError + 1001, "Read INI file failed"
            Exit Sub
        End If
        sTmpStr = Left$(sKeyValue, InStr(sKeyValue, Chr(0)) - 1)
        If sTmpStr = NULL_KEY_VALUE Or sTmpStr = "" Then
            Err.Raise vbObjectError + 1001, "Invalid INI file path"
            Exit Sub
        End If
        If sTmpStr = "Current" Then
            sIniPath = AppPath
        Else
            If Dir(sTmpStr & sIniFile) = "" Then
                Err.Raise vbObjectError + 1001, "Invalid INI file path"
                Exit Sub
            End If
            sIniPath = sTmpStr
        End If
    End SubPublic Function ReadKey(VsECTION As String, vKeyName As String) As String
        Dim iCount As Integer
        Dim sKeyValue As String * 255
        
        iCount = GetPrivateProfileString(VsECTION, vKeyName, NULL_KEY_VALUE, sKeyValue, Len(sKeyValue), sIniPath & sIniFile)
        If iCount = 0 Then
            Err.Raise vbObjectError + 1001, "Reading INI file failed"
            Exit Function
        End If
        ReadKey = Left$(sKeyValue, InStr(sKeyValue, Chr(0)) - 1)
    End FunctionPublic Sub WriteKey(VsECTION As String, vKeyName As String, Optional vKeyValue As String = "")
        WritePrivateProfileString VsECTION, vKeyName, vKeyValue, sIniPath & sIniFile
    End SubPublic Property Get InitPath() As String
        InitPath = sIniPath
    End PropertyPublic Function ReadGridSetting(VsECTION As String, vKeyName As String) As String
    Dim sCfgFile        As StringDim iCount          As Integer
    Dim sKeyValue       As String * 255
        
        
        sCfgFile = sIniPath & "\GridSetting.cfg"
        
        If Dir(sCfgFile) = "" Then  'if not found
            WritePrivateProfileString "Test", "Test", "Test", sCfgFile
        End If
        
        iCount = GetPrivateProfileString(VsECTION, vKeyName, NULL_KEY_VALUE, sKeyValue, Len(sKeyValue), sCfgFile)
        If iCount = 0 Then
            ReadGridSetting = NULL_KEY_VALUE
            Exit Function
        End If
        
        ReadGridSetting = Left$(sKeyValue, InStr(sKeyValue, Chr(0)) - 1)End FunctionPublic Sub WriteGridSetting(VsECTION As String, vKeyName As String, Optional vKeyValue As String = "")
    Dim sCfgFile        As String    sCfgFile = sIniPath & "\GridSetting.cfg"    WritePrivateProfileString VsECTION, vKeyName, vKeyValue, sCfgFile
        
    End SubPublic Function GetAllSections() As Variant
      Dim lngSize As Long
      Dim astrItems As Variant
      Dim i As Integer
      Dim astrSections As Variant
      Dim sMasterSource As String
      Dim strSections As String
    On Error GoTo HandleErrorslngSize = 1024
     Do
        
        strSections = Space$(lngSize)
        lngSize = GetPrivateProfileSectionNames(strSections, lngSize, AppPath & sIniFile)
     
     If lngSize = 0 Then
          GoTo ExitHere
       ElseIf lngSize = Len(strSections) - 2 Then
         lngSize = lngSize * 2
       Else
          strSections = Left$(strSections, lngSize - 1)
         Exit Do
        End If
     Loop
      
      astrSections = Split(strSections, vbNullChar)
      
      GetAllSections = astrSections
      
    ExitHere:
      Exit Function
      
    HandleErrors:
      Err.Raise Err.Number, Err.Source, Err.Description
    End FunctionPublic Function GetSingleSection(ByVal pSectionName As String) As Variant
    Dim strSection As String
    Dim lngSize As Long
    Dim astrSections As Variant
    Dim i As Integer  
      On Error GoTo HandleErrors
      
      lngSize = 1024
      
      Do
        
        strSection = Space$(lngSize)
        lngSize = GetPrivateProfileSection(pSectionName, strSection, lngSize, AppPath & sIniFile)
        
        If lngSize = 0 Then
          GoTo ExitHere
        ElseIf lngSize = Len(strSection) - 2 Then
          lngSize = lngSize * 2
        Else
          strSection = Left$(strSection, lngSize - 1)
          Exit Do
        End If
        
      Loop
      
      astrSections = Split(strSection, vbNullChar)         
     GetSingleSection = astrSectionsExitHere:
      Exit Function
      
    HandleErrors:
      Err.Raise Err.Number, Err.Source, Err.Description
      
    End Function
      

  8.   

    当你用WritePrivateProfileString往ini文件里写东西的时候,如果ini文件不存在就会自动创建一个ini文件。