VB6中一個非常好用的讀寫Ini文件的模塊
hejianzhong
網(wǎng)上讀寫Ini文件的例子只有幾篇相同的文章,而并不好用,奇怪的是各網(wǎng)站都是同樣的例程,高手也就罷了,三下五除二就搞定,初學(xué)者會被搞得一頭霧水,看著一個好好的模塊就是不能用。
所以我整理了一下(最早是在騰訊答一個貼子時寫的),這個也就是修改了一下,不是我自已的發(fā)明(至于這個代碼起先不知是誰寫的),不過非常的好用
新建模塊(建議不使用注冊表) 命名為rwini
‘ini文件在有回車換行符會出錯,經(jīng)過測試,漢字要小于86字節(jié),英言文要小于143字節(jié)才能返回列表框。(這是我以前的code,是記錄列表框內(nèi)容的)
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))
‘檢索關(guān)鍵詞的值
If Temp% > 0 Then ‘關(guān)鍵詞的值不為空
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數(shù)值(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
‘****************************************寫入數(shù)值(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函數(shù)寫入一個空的值實現(xiàn)的,‘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) 寫入數(shù)
‘GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long)讀取數(shù)
‘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) 讀取字符
調(diào)用例子如下:
Sub RiniN()
Dim Initemp As String
Initemp = iniFileName ‘暫存原來的Ini文件名
iniFileName = App.EXEName ‘寫入到另外一個Ini文件,App.EXEName是你的程序的名程
If GetIniN("lstBackup", "backupnumber", 0) < lstBackUp.ListCount Then
‘這里的第三個參數(shù)“0”表示在沒有找到指定的鍵值時返回的缺省值為“0”
SetIniN "lstBackup", "backupnumber", lstBackUp.ListCount
‘......
End If
iniFileName = Initemp ‘繼續(xù)使用原來的Ini文件
End Sub
‘你在你的電腦子上搜索*.ini就看一下,最上面是部,每一個key后面是一個對應(yīng)的值
再次重申,這個Ini讀寫的例子是網(wǎng)上找來改寫的,我只是為方便大家使用。算不得我的發(fā)明。(我本來不想寫VB6的東西---我本來就不曾認真學(xué)過VB6的了,實在不明白為什么要傳播一個不完整的例程這么久),如果有任何不當(dāng)?shù)牡胤?,?#8220;權(quán)且”吧。
聯(lián)系客服