现在开发的项目需要有个ini文件读写的功能,但小弟没接触过VB,请高手救急!!

解决方案 »

  1.   

    首先调用两个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写Ini文件函数:
    '---------------------------------------------------------------------------------------
    ' Procedure : subWriteINI
    ' 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
    读Ini文件函数:
    '---------------------------------------------------------------------------------------
    ' Procedure : funGetINI
    ' 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 * 2000
       Dim strTemp As String
       
       strVal = String(2000, 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
    例子:
    如ini文件的名称为Config.ini内容如下:
    [SQL SERVER]
    LOGIN=82719005548271908444;
    写ini文件:Call subWriteINI("SQL SERVER", "LOGIN", Config.ini,赋的新值 )
    读ini文件:IniValue=funGetINI("SQL SERVER", "LOGIN", Config.ini, "")