一个绝对经典的在VB中操作.ini文件的通用类源代码

goj2000 2004-01-28 10:31:55
一个绝对经典的在VB中操作.ini文件的通用类源代码

(有需要源程序的,请在留言中留下电子邮件地址)


源程序:

classIniFile.cls的内容:

Option Explicit

'--------classIniFile.cls 代码----------------
'这里定义了一个classIniFile类
'一个绝对经典的在VB中操作.ini文件的通用类源代码
'程序编写:中国青岛·许家国
' 2002.6.16
'E-Mail: goj2000@163.com
'HomePage: http://www.gojclub.com
'

'Private member that holds a reference to
'the path of our ini file

Private strINI As String

'Windows 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

Private Sub CreateIni(strDrv As String, strDir As String)
' Make a new ini file
strINI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
' Write to strINI
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub

Public Function GetIniKey(strSection As String, strKey As String) As String
Dim strTmp As String
Dim lngRet As String
Dim I As Integer
Dim strTmp2 As String

strTmp = String$(1024, Chr(32))
lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
strTmp = Trim(strTmp)
strTmp2 = ""
For I = 1 To Len(strTmp)
If Asc(Mid(strTmp, I, 1)) <> 0 Then
strTmp2 = strTmp2 + Mid(strTmp, I, 1)
End If
Next I
strTmp = strTmp2

GetIniKey = strTmp
End Function

Public Property Let INIFileName(ByVal New_IniPath As String)
' Sets the new ini path
strINI = New_IniPath
End Property

Public Property Get INIFileName() As String
' Returns the current ini path
INIFileName = strINI
End Property

'***************************************清除KeyWord"键"(Sub)***********************************************
Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function

'如果是清除section就少写一个Key多一个""。
'**************************************清除 Section"段"(Sub)***********************************************
Public Function DelIniSec(ByVal SectionName As String) '清除section
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Function



Form1中的内容:

Option Explicit

'一个绝对经典的在VB中操作.ini文件的通用类源代码示例程序
'程序编写:中国青岛·许家国
' 2002.6.16
'E-Mail: goj2000@163.com
'HomePage: http://www.gojclub.com

'定义一个.ini类型的变量
Dim DemoIni As New classIniFile

Private Sub Form_Load()
'对控件进行初始化
Text1.Text = "测试一下"
List1.Clear

'定义.ini文件名,并写入一些初始数据
DemoIni.INIFileName = App.Path & "\demoini.ini"
DemoIni.WriteIniKey "系统", "启动路径", App.Path
DemoIni.WriteIniKey "系统", "可执行程序文件名", App.EXEName

'显示保存到.ini文件中的数据
Call CmdRead_Click
End Sub

'退出程序
Private Sub CmdExit_Click()
Unload Me
End Sub

'读取.ini文件中已经存在的数据并显示出来
Private Sub CmdRead_Click()
Dim TestStr As String

List1.Clear
TestStr = DemoIni.GetIniKey("系统", "启动路径")
List1.AddItem "系统 - 启动路径: " & TestStr
TestStr = DemoIni.GetIniKey("系统", "可执行程序文件名")
List1.AddItem "系统 - 可执行程序文件名: " & TestStr
TestStr = DemoIni.GetIniKey("用户自定义", "用户数据")
List1.AddItem "用户自定义 - 用户数据: " & TestStr
End Sub

'保存用户自定义数据到.ini文件中
Private Sub CmdSave_Click()
DemoIni.WriteIniKey "用户自定义", "用户数据", Text1.Text

'显示保存到.ini文件中的数据
Call CmdRead_Click
End Sub

'清除用户自定义段和段中数据
Private Sub CmdDelete_Click()
DemoIni.DelIniKey "用户自定义", "用户数据"
DemoIni.DelIniSec "用户自定义"

'显示保存到.ini文件中的数据
Call CmdRead_Click
End Sub

...全文
140 57 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
57 条回复
切换为时间正序
请发表友善的回复…
发表回复
pigsanddogs 2004-03-21
  • 打赏
  • 举报
回复
jj.net@163.com
djfdd 2004-03-21
  • 打赏
  • 举报
回复
谢谢,djfdd@163.net
acev 2004-02-03
  • 打赏
  • 举报
回复
感谢楼主!
acev@21cn.com
Axuluo 2004-02-02
  • 打赏
  • 举报
回复
ziling_letian@163.com 谢谢!!
fangyds 2004-02-02
  • 打赏
  • 举报
回复
wwfangyd@yahoo.com.cn
vcshcn 2004-02-02
  • 打赏
  • 举报
回复
vcshcn@yahoo.com.cn
rednod 2004-02-02
  • 打赏
  • 举报
回复
来的及吗?

rednod@163.com
crs96321 2004-02-02
  • 打赏
  • 举报
回复
不知道我能不能赶上!
确实非常需要!谢谢先!
crs96321@163.com
南山明月 2004-02-02
  • 打赏
  • 举报
回复
qscandwh@163.com
goj2000 2004-02-02
  • 打赏
  • 举报
回复
up
goj2000 2004-02-01
  • 打赏
  • 举报
回复
楼上两位网友的邮件已经发出,请查收。
evbsky 2004-02-01
  • 打赏
  • 举报
回复
evbsky@sina.com谢谢
lingll 2004-01-31
  • 打赏
  • 举报
回复
ini只是文本文件,
用文件操作的那套方法,想怎样改都行,
pigsanddogs 2004-01-31
  • 打赏
  • 举报
回复
以前在evc下做过ini的读写,由于wince.net没有WritePrivateProfileString等函数
所以自己写了一个, 基本上是探测了该api函数原理用writefile,readfile等重写的
经过多次测试,优化。绝对没问题
c版本的, 需要的话可以于我联系。
ChenCCC 2004-01-31
  • 打赏
  • 举报
回复
我也要,谢谢
xpctchen@163.com
vbnewer 2004-01-31
  • 打赏
  • 举报
回复
也给我来一份,谢谢!
vvkko@sina.com
goj2000 2004-01-31
  • 打赏
  • 举报
回复
同意。
lingll 2004-01-31
  • 打赏
  • 举报
回复
我对那些往注册表写东西的程序是很反感的,
所以我写的程序只使用文件来记录数据,不写注册表
于是ini就是主要的方案了
goj2000 2004-01-31
  • 打赏
  • 举报
回复
以上网友的邮件已经发出,请查收。
真没想到有这么多网友有需求,如果大家也有好的东东,也请不要吝啬,拿出来共同分享啊。
这样可以共同提高我们中国人的编程水平和编程质量,同时也是为我们用VB编程的中国人争一口气啊,免得人家用VC的瞧不起我们。哈哈哈...
goj2000 2004-01-31
  • 打赏
  • 举报
回复
复:wenwenwen112(三万英尺)
INI是要放弃的东西了,还炫耀......

旧的东西不一定要放弃,常言说得好:酒是陈的香。我是坚决反对动不动就往注册表里写东西的,如果大家都往注册表里乱写东西,最后的结果必然是系统越来越慢,甚至瘫痪。

同意我的意思的兄弟请支持一下。
如果不同意,请说明除了注册表之外的能取代ini的办法,也让我们学习学习。
加载更多回复(37)

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧