怎样打开.ini文件?并执行相关的操作?
//////////////////////////////////////////////////////////////////////////////////////////
Open "e:\1111.ini" For Output As #1
     Do While Not EOF(1)
        Print #1, , Whole
     Loop
Close #1
//////////////////////////////////////////////////////////////////////////////////////////
提示:"路径/文件访问错误"!
如果能打开的话,是否和.txt文件的操作一样呢?
如果我只想修改其中一整行数据又该如何写程序?(文件中有好几十行数据)

解决方案 »

  1.   

    Option Explicit
    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 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
    'FileName:Ini文件
    'PathName:小节名
    'KeyName:值名
    'WriteValue:值
    Public Function WriteIni(FileName As String, _
            PathName As String, _
            KeyName As String, _
            WriteValue As String) As Long
        Dim Rc As Long
        
        Rc = WritePrivateProfileString(PathName, KeyName, WriteValue, FileName)
        
        WriteIni = Rc
        
    End Function'FileName:Ini文件
    'PathName:小节名
    'KeyName:值名
    'BackValue:返回值
    'Default:默认字符
    Public Function ReadIni(FileName As String, _
            PathName As String, _
            KeyName As String, _
            BackValue As String, _
            Optional Default As String = "缺省") As Long
        Dim Rc As Long
        Dim TempNum As String
        Dim TempStr As String
        
        TempStr = String$(255, Chr$(0))
        TempNum = 255
        
        Rc = GetPrivateProfileString(PathName, KeyName, Default, TempStr, TempNum, FileName)
        
        If Rc <> 0 Then
            BackValue = Left$(TempStr, TempNum)
            
        End If
        
        ReadIni = Rc
        
    End Function
      

  2.   

    要用API函数吗?
    好难懂哦!!!
      

  3.   

    Option ExplicitPrivate 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
    Public 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
    '自定义读取INI函數
    Private Function getIni(ByVal pfileName As String, ByVal psection As String, ByVal pkey As String) As String
        Dim x As Long, Buff As String * 128, i%
        x = GetPrivateProfileString(psection, pkey, "", Buff, 128, pfileName)
        i = InStr(Buff, Chr(0))
        getIni = Trim(Left(Buff, i - 1))
    End Function'自定义写入INI函數
    Private Function WriteIni(ByVal psection As String, ByVal pkey As String, ByVal pvalue As String, ByVal filePath As String) As Boolean
        Dim x As Long, Buff As String * 128, i As Integer
        Buff = pvalue + Chr(0)
        x = WritePrivateProfileString(psection, pkey, Buff, filePath)
        WriteIni = x
    End Function
      

  4.   

    Option Explicit
    '---------------------------------------------------------------------------------------
    ' Module    : MdlGeneral
    ' DateTime  : 2004-10-22 09:10
    ' Purpose   : INI 文件读、写模块
    '---------------------------------------------------------------------------------------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'---------------------------------------------------------------------------------------
    ' Procedure : subWriteINI
    ' DateTime  : 2004-10-22
    ' Accept    : [strHeadName] INI文件的表头,就是[]里的内容
    '           : [strListName] = 左边的名称
    '           : [strFileName] INI文件名
    '           : [strVal] = 右边的值
    '           : [strExplain] 说明,可选的
    ' Return    :
    ' Purpose   : 将信息写入Ini文件
    '---------------------------------------------------------------------------------------
    Public Sub subWriteINI(ByVal strHeadName As String, _
                        ByVal strListName As String, _
                        ByVal strFileName As String, _
                        ByVal strVal As String, _
                        Optional ByVal strExplain As String = "")
       '如果是根目录,就直接当前目录加上文件名
       If Right$(App.path, 1) = "\" Then
          strFileName = App.path & strFileName
       '如果非根目录,就是在当前目录和文件名中间加"\"
       Else
          strFileName = App.path & "\" & strFileName
       End If
       
       '判断文件是否已经存在
       If funExists(strFileName, vbNormal) = False Then
          '如果文件不存在,则创建一个新的文件
          Dim i As Integer
          i = FreeFile
          Open strFileName For Output As #i
          Close #i
       End If
       Call WritePrivateProfileString(strHeadName, strListName, _
                                      strVal & ";" & strExplain, strFileName)
    End Sub'---------------------------------------------------------------------------------------
    ' Procedure : funGetINI
    ' DateTime  : 2004-10-22
    ' Accept    : [strHeadName] INI文件的表头,就是[]里的内容
    '           : [strListName] = 左边的名称
    '           : [strFileName] INI文件名
    '           : [strDefault]
    ' Return    :
    ' Purpose   : 读INI文件过程
    '---------------------------------------------------------------------------------------
    Public Function funGetINI(ByVal strHeadName As String, _
                           ByVal strListName As String, _
                           ByVal strFileName As String, _
                           Optional ByVal strDefault As String = "") As String
       
       Dim strVal As String * 1000
       Dim strTemp As String
       
       strVal = String(1000, Space(1))
       
       Dim l As Long
       If Right$(App.path, 1) = "\" Then
          strFileName = App.path & strFileName
       Else
          strFileName = App.path & "\" & strFileName
       End If
       l = GetPrivateProfileString(strHeadName, strListName, "", _
                                    strVal, Len(strVal), strFileName)
       strTemp = ""
       If InStr(strVal, ";") > 0 Then
          strTemp = Left$(strVal, InStr(strVal, ";") - 1)
       End If
       
       If strTemp = "" Then
          funGetINI = strDefault
       Else
          funGetINI = strTemp
       End If
       
    End Function'---------------------------------------------------------------------------------------
    ' Procedure : funExists
    ' DateTime  : 2004-10-22
    ' Accept    : [strFileName] 文件名
    '           : [intVal] 判断是否为目录
    ' Return    :
    ' Purpose   : 判断文件或目录是否存在
    '---------------------------------------------------------------------------------------
    Public Function funExists(ByVal strFileName As String, _
                           ByVal intVal As VbFileAttribute) As Boolean
       
       On Error Resume Next
       
       If Len(strFileName) = 0 Then
          funExists = False
          Exit Function
       End If
       
       If intVal <> vbDirectory Then      '如果不是目录
          '如果为空表示文件不存在
          If Dir(strFileName) = "" Then
             funExists = False
          Else
             funExists = True
          End If
       Else
          If Dir(strFileName, vbDirectory) = "" Then
             funExists = False
          Else
             funExists = True
          End If
       End If
       
    End Function
    这是个读写INI文件的模块,大家可以看看
      

  5.   

    '我写的类模块
    '保存和读取INI文件的类模块'**************************** Begin 属性 *********************************
    '
    'ApplicationName
    '权限:读写
    '类型:String
    '说明:指定在保存或读取INI文件时默认的小节名称,如果不赋值或赋值空字符串(""),那么该属性默认为使用该类模块的工程标题(App.Title)
    '
    'FileName
    '权限:读写
    '类型:String
    '说明:指定在保存或读取的INI文件的文件名,如果不赋值或赋值空字符串(""),那么该属性默认为使用该类模块的工程的EXE文件名(App.EXEName)加 .ini 后缀
    '
    '**************************** End 属性 *********************************
    '
    '**************************** Begin 函数 *********************************
    '
    'Save()
    '函数介绍:保存内容到INI文件
    '返回类型:Boolean,成功返回True,失败返回False
    '参数:KeyName;Value;Optional ApplicationName;Optional FileName
    '参数类型:全部为 String
    '参数传递:全部传值
    '参数说明:要保存的项名字;要保存的项的具体值;要保存的项所在的小节名字(可选,默认值是 ApplicationName 属性);要保存内容到所在的 INI 文件的文件名(可选,默认是 FileName 属性)
    '
    'Read()
    '函数介绍:从INI文件读取内容
    '返回类型:String,成功返回读取的内容,失败返回空字符串("") (注意,成功也可能返回空字符串,因为读取的内容可能为空字符串)
    '参数:KeyName;Optional DefaultValue;Optional ApplicationName;Optional FileName
    '参数类型:全部为 String
    '参数传递:全部传值
    '参数说明:要读取内容的项名;当读取不到项内容时的默认返回值(可选,默认为空字符串(""));要读取的项所在的小节名字(可选,默认值是 ApplicationName 属性);要读取的内容所在的 INI 文件的文件名(可选,默认是 FileName 属性)
    '
    '**************************** End 函数 *********************************
    Option Explicit'手动改了两个函数的参数
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Private 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 m_strApplicationName As String
    Private m_strFileName As String
    Public Property Get ApplicationName() As String
    On Error GoTo Error    ApplicationName = m_strApplicationName
        
        Exit Property
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End PropertyPublic Property Let ApplicationName(ByVal vNewValue As String)
    On Error GoTo Error    If vNewValue = "" Then
            Exit Property
        End If
        
        m_strApplicationName = vNewValue    Exit Property
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End Property
    Public Property Get FileName() As String
    On Error GoTo Error    FileName = m_strFileName    Exit Property
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End PropertyPublic Property Let FileName(ByVal vNewValue As String)
    On Error GoTo Error    If vNewValue = "" Then
            Exit Property
        End If
            
         m_strFileName = FileName
        
        Exit Property
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End PropertyPrivate Sub Class_Initialize()
    On Error GoTo Error    m_strApplicationName = App.Title
        
        m_strFileName = App.Path
        If Right$(m_strFileName, 1) <> Chr$(92) Then
            m_strFileName = m_strFileName & Chr$(92)
        End If
        m_strFileName = m_strFileName & App.EXEName & ".ini"
        
        Exit Sub
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End SubPublic Function Save(ByVal KeyName As String, ByVal Value As String, Optional ByVal ApplicationName As String, Optional ByVal FileName As String) As Boolean
    On Error GoTo Error    If ApplicationName = "" Then
            ApplicationName = m_strApplicationName
        End If
        
        If FileName = "" Then
            FileName = m_strFileName
        End If
        Save = (WritePrivateProfileString(ApplicationName, KeyName, Value, FileName) <> 0)    Exit Function
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End FunctionPublic Function Read(ByVal KeyName As String, Optional ByVal DefaultValue As String, Optional ByVal ApplicationName As String, Optional ByVal FileName As String)
    On Error GoTo Error    Dim buf As String
        Dim bufLen As Long
        Dim ret As Long
        
        If ApplicationName = "" Then
            ApplicationName = m_strApplicationName
        End If
        
        If FileName = "" Then
            FileName = m_strFileName
        End If
        
        bufLen = 255
        buf = String$(bufLen, Chr$(0))
        ret = GetPrivateProfileString(ApplicationName, KeyName, DefaultValue, buf, bufLen, FileName)
        If ret = 0 Then
            Exit Function
        End If
        Read = Left$(buf, InStr(1, buf, Chr$(0)) - 1)    Exit Function
    Error:
        MsgBox Err.Description, vbInformation
        Resume Next
    End Function
      

  6.   

    使用前声明:
    Dim m_clsSaveINI As New ClassSaveINI
    读取的例子:    Me.Left = m_clsSaveINI.Read("左边距", 0)
        Me.Top = m_clsSaveINI.Read("上边距", 0)保存的例子:
        m_clsSaveINI.Save "左边距", Me.Left
        m_clsSaveINI.Save "上边距", Me.Top
      

  7.   

    Call subWriteINI("JIABAN", "养老保险保底标准", gc_strConfigFile, Field1)向其中写入数据