[吐血奉献]VB使用系统函数读写INI配置文件的模块

Attribute VB_Name = “cls_Ini”
‘========================================
‘模块名称:cls_Ini
‘函数作用:读写INI文件
‘函数作者:纪小年 http://8jxn.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
‘lpApplicationName  字段名
‘lpKeyName          键名
‘lpString           键值
‘lpFileName         INI文件路径
‘lpDefault          缺省值
‘lpReturnedString   缓冲区
‘nSize              缓冲区大小

 

‘========================================
‘函数名称:GetIniS
‘函数作用:从INI配置文件中读取指定段的字符串
‘a = GetIniS(“Class”, “QXWclasschild32”, “20”)
‘========================================
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文件的路径
‘a = Path
‘Path = “C:1.INI”
‘========================================
Public Property Get Path() As String
Path = iniFileName
End Property
Public Property Let Path(ByVal sPath As String)
iniFileName = sPath
End Property

您可能还喜欢...

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据