建立與讀取.ini檔案

ljm0211發表於2012-06-26
雖然進入win95之後,一般讀寫ini檔案被讀寫Registry所取代,但我們還是可以透過
win31的傳統方式讀寫ini檔案,以存程式目前的相關設定,而於下一次程式執行時再
讀回來。目前建議使用GetSetting SaveSetting的方式存於Registry中,不用目前
的方式。 儲存程式的設定

'請於form中放3個TextBox,一個CommandBox
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 WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long

Private Sub Command1_Click()
Dim success As Long
success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini")
'叄數一 Section Name
'叄數二 於.ini中的專案
'叄數三 專案的內容
'叄數四 .ini檔案的名稱
success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini")
success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini")
End Sub

Private Sub Form_load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini")
'若.ini MyApp中無text1,則採用叄數三的值
Text1.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
Text2.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
Text3.Text = buff
End Sub

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11411056/viewspace-733853/,如需轉載,請註明出處,否則將追究法律責任。

相關文章