程序代码:
'读写INI文件函数
Option Explicit
'写
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 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
'保存到INI文件
Public Function Wini(ByVal 段 As String, ByVal 键 As String, ByVal 值 As String, IniFile As String) As Boolean
Dim X As Long, buff As String * 1024, i As Long
buff = 值 & Chr(0)
X = WritePrivateProfileString(段, 键, buff, IniFile)
Wini = X
End Function
'从INI文件中读取
Public Function Rini(ByVal 段 As String, ByVal 键 As String, IniFile As String) As String
Dim X As Long, buff As String * 1024, i As Long
X = GetPrivateProfileString(段, 键, "", buff, 1024, IniFile)
i = InStr(buff, Chr(0))
Rini = Trim(Left(buff, i - 1))
End Function
写
i=Wini("节名称","关键字名称","内容","INI文件名")
i得到的是否成功的标志。一般情况下不用检查。
读
dim i as string
i=rini("节名称","关键字名称","INI文件名")
i 得到的是 内容。再自己转换为所需要的格式,如数值。