Minggu, 29 April 2012

Membuat Membaca Menulis INI File Read Write pada VB.NET

Terkadang kita harus membuat file INI File sebagai sarana penyimpan setting program yang dapat diubah-ubah walaupun program sudah di compile. Misalnya, setting database, setting koneksi dan sebagainya. Pada VB.NET kita harus memakai Windows API untuk membuat, membaca dan menulis INI Files.

Deklarasikan dahulu fungsi Windows API berikut :

    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
    Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpString As String, _
    ByVal lpFileName As String) As Int32

    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Int32, _
    ByVal lpFileName As String) As Int32

    Public Function INIRead(ByVal INIPath As String, _
    ByVal SectionName As String, ByVal KeyName As String, _
    ByVal DefaultValue As String) As String
        Dim n As Int32
        Dim sData As String
        sData = Space$(1024)

        n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
        sData, sData.Length, INIPath)
        If n > 0 Then  
            INIRead = sData.Substring(0, n)
        Else
            INIRead = ""
        End If
    End Function

    Public Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String, _
    ByVal KeyName As String, ByVal TheValue As String)
        Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
    End Sub

    Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String, _
    ByVal KeyName As String)

        Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
    End Sub


Berikut ini cara untuk menggunakan fungsi untuk membaca INI Files

        Dim sValue As String
        Dim sPath As String
        sPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\setting.ini"
        Dim server As String = INIRead(sPath, "Setting", "Server", "127.0.0.1\AUDI")
        Dim username As String = INIRead(sPath, "Setting", "Username", "sa")
        Dim password As String = INIRead(sPath, "Setting", "Password", "root")
        Dim database As String = INIRead(sPath, "Setting", "Database", "Manufaktur")

        txtServerHost.Text = server
        txtUsername.Text = username
        txtPassword.Text = password
        txtDB.Text = database


Berikut ini cara untuk menggunakan fungsi untuk menulis INI Files

        Dim sValue As String
        Dim sPath As String
        sPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\setting.ini"

        INIWrite(sPath, "Setting", "Server", txtServerHost.Text) ' build INI file 
        'INIWrite(sPath, "Section1", "Key1-2", "Value1-2")
        'INIWrite(sPath, "Section1", "Key1-3", "Value1-3")
        INIWrite(sPath, "Setting", "Username", txtUsername.Text)
        INIWrite(sPath, "Setting", "Password", txtPassword.Text)
        INIWrite(sPath, "Setting", "Database", txtDB.Text)