VB源码推荐:一个操作Ini文件的类 (转)

本文提供了一个使用VB编写的类,用于操作Ini配置文件。该类包括创建Ini文件、写入配置项及读取配置项等功能。通过示例展示了如何初始化类并进行基本的读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VB源码推荐:一个操作Ini文件的类 (转)[@more@]

【VB源码推荐:一个操作Ini文件的类】

'--------cIniFile.cls 代码----------------
'这里定义了一个cIniFile类
Option Explicit

'// Private member that holds a reference to
'// the path of our ini file
Private strInI As String

'// Win api Declares
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

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 Function MakePath(ByVal strDrv As String, _
 ByVal strDir As String) As String

'// Makes an INI file: Guarantees a sub dir
Do While Right$(strDrv, 1) = ""
 strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop

Do While Left$(strDir, 1) = ""
 strDir = Mid$(strDir, 2)
Loop

'// Return the path
MakePath = strDrv & "" & strDir
End Function

Public Sub CreateIni(strDrv As String, strDir As String)


'// Make a new ini file
strInI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteFile(strSection As String, _
strKey As String, _
strValue As String)

'// Write to strINI
WritePrivateProfileString strSection, _
 strKey, strValue, strInI
End Sub

Public Function GetFile(strSection As String, _
 strKey As String) As String

 Dim strTmp As String
 Dim lngRet As String

 strTmp = String$(100, Chr(32))
 lngRet = GetPrivateProfileString(strSection, _
 strKey, "", strTmp, _
 Len(strTmp), strInI)
 
 GetFile = strTmp

End Function

Public Property Let INIFile(ByVal New_IniPath As String)

'// Sets the new ini path
strInI = New_IniPath

End Property

Public Property Get INIFile() As String

'// Returns the current ini path
INIFile = strInI

End Property

'--------cIniFile.cls 使用举例----------------

 Dim myIniFile As New cIniFile
 
 '---指定访问的ini文件
 If Len(App.Path) > 3 Then
 'under disk root dir , eg: "C:"
 myIniFile.INIFile = App.Path & "setting.ini"
 Else
 myIniFile.INIFile = App.Path & "setting.ini"
 End If
 
 '---写入ini文件
 myIniFile.WriteFile "setting", "username", strUser
 
 '---读出ini文件的数据
 ' 注意,如果是字符串,则去掉末尾一个字符
 ' ----flybird@chinaASP.com
 strUser = Trim(myIniFile.GetFile("setting", "username"))
 strUser = Left(strUser, Len(strUser) - 1)


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-987495/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-987495/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值