VB讀寫ini文件
發(fā)表時(shí)間:2023-07-27 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]自從注冊表誕生以來ini文件正在逐漸失去其市場占有率,然而基于ini文件的獨(dú)立性,致使其還沒有到達(dá)退出歷史舞臺的地步,很多應(yīng)用程序的初始化和一些界面參數(shù)的設(shè)置仍然很愿意從ini文件中讀取,為了保證操...
自從注冊表誕生以來ini文件正在逐漸失去其市場占有率,然而基于ini文件的獨(dú)立性,致使其還沒有到達(dá)退出歷史舞臺的地步,很多應(yīng)用程序的初始化和一些界面參數(shù)的設(shè)置仍然很愿意從ini文件中讀取,為了保證操作需用參數(shù)對ini文件的讀取的通明性,建議使用一個(gè)模塊來完成此工作。注:所有操作調(diào)用標(biāo)準(zhǔn)的Win API函數(shù)來完成。
Dim Ret As Long
Dim Start As Long
Public FileName As String
Const BufSize = 10240
Dim buf As String * BufSize
Private 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
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, 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 Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
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
Public Sub SetValue(ByVal clsName As String, ByVal key As String, ByVal V As String)
Ret = WritePrivateProfileString(clsName, key, V, FileName)
End Sub
Public Function GetValue(ByVal clsName As String, ByVal key As String) As String
Ret = GetPrivateProfileString(clsName, key, "", buf, BufSize, FileName)
Start = 1
GetValue = RetStr()
End Function
Private Function RetStr() As String
Dim i As Long
i = InStr(Start, buf, Chr(0))
If i > Start Then
RetStr = Mid(buf, Start, i - Start)
End If
Start = i + 1
End Function
至此已經(jīng)完成了對一個(gè)完整的獨(dú)立模塊的封裝,接下來就來看看怎么引用(其實(shí)看完上面程序就明了了)