VB怎么样读写ini文件,请举个例子,如果有封装的类就更好了,谢谢啊!!!!!

解决方案 »

  1.   

    Easy INI File Access
    http://www.vbaccelerator.com/home/VB/Code/Libraries/Registry_and_Ini_Files/Easy_Ini_File_Access/article.asp
    http://www.vbaccelerator.com/home/VB/Code/Libraries/Registry_and_Ini_Files/Easy_Ini_File_Access/cIniFile_Class.asp
    http://www.vbaccelerator.com/home/VB/Code/Libraries/Registry_and_Ini_Files/Easy_Ini_File_Access/cIniFile_Demonstration_Application.asp
      

  2.   

    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
    Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer 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 Long
      

  3.   

    读写ini的通用模块:
    Option Explicit
    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
    Public Function GetINI(strINIFile As String, strSection As String, _
                strKey As String, strDefault As String)
    On Error GoTo ErrMsg
        Dim strTemp As String
        Dim intLength As Integer
        
        '判断INI文件是否存在
        If Dir(strINIFile) = "" Then
            MsgBox "INI文件“" & strINIFile & "”已被损坏,请联系管理人员或开发人员!", vbExclamation, "警告!"
    '        RepairINIFile strINIFile
            Exit Function
        End If
        strTemp = Space$(256)
        intLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, 255, strINIFile)
        GetINI = Left$(strTemp, intLength)
        Exit Function
    ErrMsg:
        MsgBox "从INI文件“" & strINIFile & "”文件读取数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End FunctionPublic Function WriteINI(strINIFile As String, strSection As String, _
                strKey As String, strValue As String) As Boolean
    On Error GoTo ErrMsg
        Dim n As Integer
        
        WriteINI = False
        'Replace any CR/LF characters with spaces
        If Len(strValue) >= 1 Then
            For n = Len(strValue) To 1
                If Mid$(strValue, n, 1) = vbCr Or Mid(strValue, n, 1) = vbLf Then
                    Mid$(strValue, n, 1) = ""
                End If
            Next n
        End If
        
        n = WritePrivateProfileString(strSection, strKey, strValue, strINIFile)
        WriteINI = True
        Exit Function
    ErrMsg:
        MsgBox "向INI文件“" & gstrCurrPath & strINIFile & "”文件写入数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
                Err.Description, vbExclamation, "提示"
    End Function