网上读写Ini文件的例子只有几篇相同的文章,而并不好用,奇怪的是各网站都是同样的例程,高手也就罢了,三下五除二就搞定,初学者会被搞得一头雾水,看着一个好好的模块就是不能用。
所以我整理了一下(最早是在腾讯答一个贴子时写的),这个也就是修改了一下,不是我自已的发明(至于这个代码起先不知是谁写的),不过非常的好用
新建模块(建议不使用注册表) 命名为rwini
'ini文件在有回车换行符会出错,经过测试,汉字要小于86字节,英言文要小于143字节才能返回列表框。(这是我以前的code,是记录列表框内容的)
Option Explicit
Public iniFileName As String
Public 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
Public 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
Public 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
'****************************************获取Ini字符串值(Function)******************************************
Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String
Dim ResultString As String * 144, Temp As Integer
Dim s As String, i As Integer
Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, AppProFileName(iniFileName))
'检索关键词的值
If Temp% > 0 Then '关键词的值不为空
s = ""
For i = 1 To 144
If Asc(Mid$(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s & Mid$(ResultString, i, 1)
End If
Next
Else
Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, AppProFileName(iniFileName))
'将缺省值写入INI文件
s = DefString
End If
GetIniS = s
End Function
'**************************************获取Ini数值(Function)***************************************************
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, AppProFileName(iniFileName))
If d <> DefValue Then
s = "" & d
d = WritePrivateProfileString(SectionName, KeyWord, s, AppProFileName(iniFileName))
End If
End Function
'***************************************写入字符串值(Sub)**************************************************
Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)
Dim res%
res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, AppProFileName(iniFileName))
End Sub
'****************************************写入数值(Sub)******************************************************
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$, AppProFileName(iniFileName))
End Sub
''这是我自已不知道怎样清除一个键(keyword) 时
写的一个清除字符串值的过程,是有write函数写入一个空的值实现的,'Sub DelIniS(ByVal SectionName As String, ByVal KeyWord As String)
'Dim retval As Integer
'retval = WritePrivateProfileString(SectionName, KeyWord, "", AppProFileName(iniFileName))
'End Sub
'其实0&表示前面的一个被清除,我多写了一个“”,如果是清除section就少写一个Key多一个“”。
'***************************************清除KeyWord"键"(Sub)*************************************************
Sub DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, AppProFileName(iniFileName))
End Sub
'如果是清除section就少写一个Key多一个“”。
'**************************************清除 Section"段"(Sub)***********************************************
Sub DelIniSec(ByVal SectionName As String) '清除section
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, "", AppProFileName(iniFileName))
End Sub
'*************************************定义Ini文件名(Function)***************************************************
'定义ini文件名
Function AppProFileName(iniFileName)
AppProFileName = App.Path & "\" & iniFileName & ".ini"
End Function
#######################################################################
'用法: 首先 定义iniFileName="文件名" 不需要 加ini后缀
'这就是说,你可以赋值给iniFileName就可以写入记录,而且你可以随时写入不同的ini文件(不管这个文件是否已存在),通过修改这个公用变量。
'然后 DelInikey(ByVal SectionName As String, ByVal KeyWord As String) 清除键
'DelIniSec(ByVal SectionName As String)) 清除部
'SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Long) 写入数
'GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long)读取数
'SetIniS (ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String) 写入字符
'GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String) 读取字符
调用例子如下:
Sub RiniN()
Dim Initemp As String
Initemp = iniFileName '暂存原来的Ini文件名
iniFileName = App.EXEName '写入到另外一个Ini文件,App.EXEName是你的程序的名程
If GetIniN("lstBackup", "backupnumber", 0) < lstBackUp.ListCount Then
'这里的第三个参数“0”表示在没有找到指定的键值时返回的缺省值为“0”
SetIniN "lstBackup", "backupnumber", lstBackUp.ListCount
'......
End If
iniFileName = Initemp '继续使用原来的Ini文件
End Sub
'你在你的电脑子上搜索*.ini就看一下,最上面是部,每一个key后面是一个对应的值
再次重申,这个Ini读写的例子是网上找来改写的,我只是为方便大家使用。算不得我的发明。(我本来不想写VB6的东西---我本来就不曾认真学过VB6的了,实在不明白为什么要传播一个不完整的例程这么久),如果有任何不当的地方,就“权且”吧。
评论