Public Class MyCls
'--------------------------------------------------
'功能:读出或设置指定INI文件中的指定条目的值
'--------------------------------------------------
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Int32, _
ByVal lpFileName As String) As Int32
'功能:从 INI 文件中指定的条目获取一个整数值
'输出:找到的条目的值;
' 如指定的条目未找到,就返回默认值。
' 如找到的数字不是一个合法的整数,函数会返回其中合法的一部分。
' 如,对于“xyz=55zz”这个条目,函数返回55
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Int32
Private strIniFlName As String '内部私有变量,存放获得的ini文件的路径
Public Property FlName() As String
Get '获得该属性的当前值
Return strIniFlName
End Get
Set(ByVal Value As String) '设置该属性的当前值
strIniFlName = Value '将外部的赋值存储到类内部的私有变量中
End Set
End Property
Public Function GetValue_Str(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
'如果指定条目的值不为空,则返回该值;
'如果为空,将默认值赋给该条目,并返回默认值
Dim strResult As String, intTmpCnt As Integer
Dim strCharact As String, intCnt As Integer
strResult = Space(255)
intTmpCnt = GetPrivateProfileString(SectionName, KeyWord, "", strResult, 255, strIniFlName)
'检索关键词的值
If intTmpCnt > 0 Then '关键词的值不为空
strCharact = ""
For intCnt = 1 To 255
If Asc(Mid(strResult, intCnt, 1)) = 0 Then '如果遇到空格,则认为结束
Exit For
Else
strCharact = strCharact & Mid(strResult, intCnt, 1)
End If
Next
Else
'将缺省值写入INI文件
'intTmpCnt = WritePrivateProfileString(SectionName, KeyWord, DefString, strIniFlName)
strCharact = DefString
End If
Return strCharact
End Function
Public Function GetValue_Int(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Integer) As Integer
Dim lngDefValue As Int32
'返回整型值
lngDefValue = GetPrivateProfileInt(SectionName, KeyWord, DefValue, strIniFlName)
Return lngDefValue
End Function
Public Sub SetValue_Str(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
Dim lngReturnValue As Int32
lngReturnValue = WritePrivateProfileString(SectionName, KeyWord, ValStr, strIniFlName)
End Sub
Public Sub SetValue_Int(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Integer)
Dim lngReturnValue As Int32, ValStr As String
ValStr = CStr(ValInt)
lngReturnValue = WritePrivateProfileString(SectionName, KeyWord, ValStr, strIniFlName)
End Sub
End Class