VB读写INI文件的模块

插入一个模块,输入以下代码,就可以用 WriteINI 和 getini 函数,
ApplicationName是INI里的[xxxx]
KeyName 就是INI里的  xxx=1231232
FileName是文件名

例如
Sub Form_load()
WriteINI “e”, “f”, “DF”, “c:\a.ini”
Msgbox getini( “e”, “f”, , “c:\a.ini” )
End Sub

模块代码如下
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
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
Public Function GetINI(ApplicationName As String, KeyName As String, Optional ByVal Default As String = “”, Optional ByVal FileName As String = “config.ini”) As String
Dim Retval As Long
Dim Value As String
FileName = IIf(Mid(FileName, 2, 2) = “:\”, FileName, Replace(App.Path & “\” & FileName, “\\”, “\”))
Value = Space(128)
Retval = GetPrivateProfileString(ApplicationName, KeyName, Default, Value, Len(Value), FileName)
GetINI = Left(Trim(Value), Len(Trim(Value)) – 1)
End Function

Public Sub WriteINI(ApplicationName As String, KeyName As String, Value As String, Optional ByVal FileName As String = “config.ini”)
Dim Retval As Long
FileName = IIf(Mid(FileName, 2, 2) = “:\”, FileName, Replace(App.Path & “\” & FileName, “\\”, “\”))
Retval = WritePrivateProfileString(ApplicationName, KeyName, Value, FileName)
End Sub



发表评论

电子邮件地址不会被公开。 必填项已用*标注