Gen-X Blog

Generate Your Working with Internet

Buat file *.ini di Visual Basic 6.0

Langsung aja ke Codenya :
:: Buat file ini, Isikan seperti yang dibawah ini :
[SETTING]
SERVER = localhost
DBNAME = dbtest
UID = root
PWD = PASSWORD

::  Tempatkan fungsi berikut di modul :

‘Ambil fungsi API
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

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

‘Buat fungsi  buat baca file ini
Public Function ReadIniValue(ByVal fileName As String, ByVal sectionHeader As String, ByVal  _
variableName As String, ByVal strDefault As String) As String
Dim buffer  As String
Dim x       As Long
buffer = String(255, 0)
x = GetPrivateProfileString(sectionHeader, variableName, strDefault, _
buffer, 255, fileName)
If x <> 0 Then
buffer = Left$(buffer, x)
ReadIniValue = buffer
Else
ReadIniValue = “”
End If
End Function

‘Buat fungsi tulis  file ini
Private Sub WriteIniValue (ByVal fileName As String, ByVal sectionHeader As String,  _
ByVal variableName As String, ByVal value As String)
Dim ret As Long
ret = WritePrivateProfileString(sectionHeader, variableName, value,  _
fileName)
End Sub

::  Buat form baru, tambahkan 2 buah Command Button, berikan nama, misalkan dengan nama cmd1 dan cmd2 :

Masukkan code berikut ini :
Private Const INI_FILE  As String = “Setting.ini”
Private Sub cmd1_Click()
Call WriteIniValue(App.Path & “\” & INI_FILE, “SETTING”, “SERVER”, “192.168.1.50”)
Call WriteIniValue(App.Path & “\” & INI_FILE, “SETTING”, “DBNAME”, “dbtest2”)
Call WriteIniValue(App.Path & “\” & INI_FILE, “SETTING”, “UID”, “test”)
Call WriteIniValue(App.Path & “\” & INI_FILE, “SETTING”, “PWD”, “testtttttt”)
MsgBox “Ok….”, vbInformation, “Info”
End Sub

Private Sub cmd2_Click()
MsgBox “Server Name : ” & ReadIniValue(App.Path & “\” & INI_FILE, “SETTING”, “SERVER”, “”) & vbCrLf & _
“DB Path : ” & ReadIniValue(App.Path & “\” & INI_FILE, “SETTING”, “DBNAME”, “”)
End Sub

:: Tempatkan file INI satu folder dengan aplikasi.
:: File testing dapat didownload disini ( Via Ziddu).
:: Tutorial dapat didownload disini (Via Ziddu).

SELAMAT MENCOBA…

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: