Friday, November 9, 2007

read write ini file



some application need configuration setting.
usually we use ini file in windows system.
vb can do both read and write ini file.
here is code for read/write ini file.

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

Const ini_file = "C:\contoh.ini"

Private Sub Command1_Click()
'this code will save from di Text1
'under section 'coba' on Key 'oke'.
hasil = WritePrivateProfileString("coba", "oke", Text1.Text, ini_file)
End Sub

Private Sub Command2_Click()
Dim my_val As String * 20
'this code will get from key 'oke' under section 'coba',
'and give to variable named my_val.
hasil = GetPrivateProfileString("coba", "oke", "Empty", my_val, Len(my_val),ini_file)
MsgBox my_val, vbInformation, "ok"
End Sub

click here to download the project...

No comments: