VB下的INI配置文件读写类模块
代码如下
-
'========================================
-
'模块名称:cls_Ini
-
'函数作用:读写INI文件,明晨网络mingchennet.com收集整理
-
'========================================
-
Option Explicit
-
Private iniFileName As String
-
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
-
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
-
-
'========================================
-
'函数名称:GetIniS
-
'函数作用:从INI配置文件中读取指定段的字符串
-
'========================================
-
Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
-
Const ProStringLen = 8192
-
Dim Res As Long, S As String, i As Long
-
S = Space(ProStringLen)
-
Res = GetPrivateProfileString(SectionName, KeyWord, DefString, S, ProStringLen, iniFileName)
-
S = Trim(Left(S, Res))
-
If Len(S) > 0 Then
-
-
i = Len(S)
-
Do While Mid(S, i, 1) = vbNullChar
-
i = i - 1
-
Loop
-
S = Left(S, i)
-
Else
-
SetIniS SectionName, KeyWord, DefString
-
S = DefString
-
End If
-
GetIniS = Trim(S)
-
End Function
-
-
'========================================
-
'函数名称:GetIniN
-
'函数作用:从INI配置文件中读取指定段的数值
-
'========================================
-
Public Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long) As Integer
-
Dim d As Long, S As String
-
d = DefValue
-
GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, iniFileName)
-
If d <> DefValue Then
-
S = "" & d
-
d = WritePrivateProfileString(SectionName, KeyWord, S, iniFileName)
-
End If
-
End Function
-
-
'========================================
-
'函数名称:SetIniS
-
'函数作用:将字符串写入指定段
-
'========================================
-
Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
-
Dim Res%
-
Res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, iniFileName)
-
End Sub
-
-
'========================================
-
'函数名称:SetIniN
-
'函数作用:将数值写入指定段
-
'========================================
-
Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Long)
-
Dim Res%, S$
-
S$ = Str$(ValInt)
-
Res% = WritePrivateProfileString(SectionName, KeyWord, S$, iniFileName)
-
End Sub
-
-
-
'========================================
-
'函数名称:DelIniKey
-
'函数作用:清除指定段的值
-
'========================================
-
Public Sub DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
-
Dim RetVal As Integer
-
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, iniFileName)
-
End Sub
-
-
-
'========================================
-
'函数名称:DelIniSec
-
'函数作用:清除指定段
-
'========================================
-
Public Sub DelIniSec(ByVal SectionName As String)
-
Dim RetVal As Integer
-
RetVal = WritePrivateProfileString(SectionName, 0&, "", iniFileName)
-
End Sub
-
-
'========================================
-
'属性名称:Path
-
'属性作用:INI文件的路径
-
'========================================
-
Public Property Get Path() As String
-
Path = iniFileName
-
End Property
-
Public Property Let Path(ByVal sPath As String)
-
iniFileName = sPath
-
End Property
-
-
-
-
-
文章源自:明晨网络,网络,《VB下的INI配置文件读写类模块》,http://www.mingchennet.com/tec/code/vb/20.htm