File .ini merupakan cara standar untuk menyimpan setting program pada kebanyakan program. Tetapi bagaimana cara membaca file .ini menggunakan Vb.net ? Berikut script yang saya dapat di internet dengan sedikit modifikasi untuk memudahkan penggunaan.
Option
Strict OnModule
INIAccess#
Region "API Calls" 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#
End Region Public Overloads 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 StringsData = Space$(1024)
n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
sData, sData.Length, INIPath)
If n > 0 ThenINIRead = sData.Substring(0, n)
ElseINIRead =
"" End If End Function#
Region "INIRead Overloads" Public Overloads Function INIRead(ByVal INIPath As String, _ ByVal SectionName As String, ByVal KeyName As String) As String Return INIRead(INIPath, SectionName, KeyName, "") End Function Public Overloads Function INIRead(ByVal INIPath As String, _ ByVal SectionName As String) As String Return INIRead(INIPath, SectionName, Nothing, "") End Function Public Overloads Function INIRead(ByVal INIPath As String) As String Return INIRead(INIPath, Nothing, Nothing, "") End Function#
End Region 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 Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String) Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath) End Sub Public Function BacaIni(ByVal Section As String, ByVal Kunci As String, ByVal IsiDefault As String) As String Dim Sisi As StringSisi = INIRead(
My.Application.Info.DirectoryPath & "\set.conf", Section, Kunci, IsiDefault)BacaIni = Sisi
End Function Public Sub TulisIni(ByVal Section As String, ByVal Kunci As String, ByVal Datanya As String)INIWrite(
My.Application.Info.DirectoryPath & "\set.conf", Section, Kunci, Datanya) End SubEnd
ModulePada Form sisipkan script berikut untuk membaca file
Dim IsiIni as string
IsiIni = BacaIni("Section", "Kunci",
"NGGAK ADA")Pada Form sisipkan script berikut untuk menulis file
TulisIni("Section", "Kunci", "Isi datanya disini")
Semoga tulisan ini bermanfaat
Tidak ada komentar:
Posting Komentar