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
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
读写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
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
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
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