為了方便使用者使用和使系統具有靈活性,大多數Win-dows應用程式將使用者所做的選擇以及各種變化的系統資訊記錄在初始化(INI)檔案中。因此,當系統的環境發生變化時,可以直接修改INI檔案,而無需修改程式。由此可見,INI檔案對系統功能是至關重要的。本文將介紹採用VisualBasicforWindows(下稱VB)開發Windows應用程式時如何讀寫INI檔案。
INI檔案是文字檔,由若干部分(section)組成,在每個帶括弧的標題下面,是若干個以單個單詞開頭的關鍵詞(keyword)和一個等號,每個關鍵詞會控制應用程式某個功能的工作方式,等號右邊的值(value)指定關鍵詞的操作方式。其一般形式如下:
[Section1]
KeyWord1=Valuel
KeyWord2=Value2
……
[Section2]
KeyWord1=Value1
KeyWord2=Value2
……
其中,如果等號右邊無任何內容(即Value為空白),那就表示Windows應用程式已為該關鍵詞指定了預設值,如果在整個檔案中找不到某個關鍵詞(或整個一部分),那同樣表示為它們指定了預設值。各個部分所出現的順序是無關緊要的,在每一個部分裡,各個關鍵詞的順序同樣也無關緊要。
讀寫INI檔案通常有兩種方式:一是在Windows中用“記事本”(Notepad)對其進行編輯,比較簡單,無需贅述;二是由Windows應用程式讀寫INI檔案,通常是應用程式運行時讀取INI檔案中的資訊,退出應用程式時儲存使用者對運行環境的某些修改。
關鍵詞的值的類型多為字串或整數型,應分兩種情況讀寫。為了使程式具有可維護性和可移植性,最好把對INI檔案的讀寫封裝在一個模組(RWINI.BAS)中,在RWI-NI.BAS中構造GetIniS和GetIniN函數以及SetIniS和Se-tIniN過程,在這些函數和過程中需要使用WindowsAPI的“GetPrivateprofileString”、“GetPrivateProfileInt”和“WritePrivateProfileString”函數。
RWINI.BAS模組的程式碼如下:
在General-Declearation部分中聲明使用到的WindowsAPI函數:
Public Declare Function GetprivateprofileString Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As _
String,ByVal lpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer, _
ByVal Filename As String)As Integer
Public Declare Function GetPrivatePfileInt Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As String, _
ByVal lpDefault As Integer,ByVal Filename As String)As Integer
Public Declare Funciton WritePrivateprofileString Lib“Kernel”(ByVal lpApplicationName As String, _
ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As Integer
Public Function GetIniS(ByVal SectionName As String,ByValKeyWord 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())
’檢索關鍵詞的值
If Temp%>0 Then ’關鍵詞的值不為空白
s=""
For i=1 To 144
If Asc(Mid$(ResultString,I,1))=0 Then
ExitFor
Else
s=s & Mid$(ResultString,I,1)
End If
Next
Else
Temp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())
’將預設值寫入INI檔案
s=DefString
End If
GetIniS=s
End Function
Public Function GetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefValue As Ineger)As Integer
Dim d As Long,s As String
d=DefValue
GetIniN=GetPrivateProfileInt(SectionName,
KeyWord,DefValue,ppProfileName())
If d<>DefValue Then
s=""
d=WritePrivateProfileString(SectionName,
KeyWord,s,AppProfileName())
End If
End Function
Public Sub SetIniS(ByVal SectionName As String,BtVal KeyWord As String,ByVal ValStr As String)
Dim res%
res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())
End Sub
Public Sub SetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal ValInt As Integer)
Dim res%,s$
s$=Str$(ValInt)
res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())
End Sub
SectionName為每一部分的標題,KeyWord為關鍵詞,GetIniS和GetIniN中的DefValue為關鍵詞的預設值,SetIniS和SetIniN的ValStr和ValInt為要寫入INI檔案的關鍵詞的值。為了能更好地說明如何使用以上函數和過程,下面舉兩個執行個體。
執行個體1:
開發應用程式通常要使用資料庫和其它一些檔案,這些檔案的目錄(包括路徑和檔案名稱)不應在程式中固定,而是儲存在INI檔案中,程式運行時由INI檔案中讀入。讀入資料庫檔案的代碼如下:
Dim Databasename As String
Databasename=GetIniS(“資料庫”,“職工”,"")
If DatabaseName="" Then DatabaseName=InputBox(“請輸入資料庫《職工》的目錄”),App.Title)
’也可通過“檔案對話方塊”進行選擇
On Error Resume Next
Set db=OpenDatabas(DatabaseName)
If Err<>0 Then
MsgBox“開啟資料庫失敗!”,MB-ICONSTOP,App.Title:GotoErrorProcessing
Else
Set IniS“資料庫”,“職工”,DatabaseName
End If
On Error GoTo 0
……
執行個體2:
為了方便使用者操作,有時需要儲存使用者介面的某些資訊,例如視窗的高度和寬度等。裝載表單時,從INI檔案中讀入表單高度和寬度,卸載表單時將表單當前高度和寬度存入INI檔案,代碼如下:
Private Sub Form1_Load()
……
Forml.Height=GetIniN(“表單1”,“高度”,6000)
Form1.Width=GetIniN(“表單1”,“高度”,4500)
EndSub
……
Private Sub Form1_Unload()
……
SetIniN“表單1”,“高度”,Me.Height
SetIniN“表單1,”寬度“,Me.Width
……
End Sub