INI Handling Using VB.NET 2008
Posted by DremationJan 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer Private Declare Ansi Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer Private Declare Ansi Function FlushPrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As Integer, ByVal lpKeyName As Integer, ByVal lpString As Integer, ByVal lpFileName As String) As Integer Dim sFile As String ReadOnly Property FileName() As String Get Return sFile End Get End Property Public Function GetString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String) As String Dim nCharCount As Integer Dim oResult As New System.Text.StringBuilder(256) nCharCount = GetPrivateProfileString(Section, Key, [Default], oResult, oResult.Capacity, sFile) If nCharCount > 0 Then End If GetString = Strings.Left(oResult.ToString, nCharCount) End Function Public Function GetInteger(ByVal Section As String, ByVal Key As String, ByVal [Default] As Integer) As Integer Return GetPrivateProfileInt(Section, Key, [Default], sFile) End Function Public Function GetBoolean(ByVal Section As String, ByVal Key As String, ByVal [Default] As Boolean) As Boolean Return (GetPrivateProfileInt(Section, Key, CInt([Default]), sFile) = 1) End Function Public Sub WriteString(ByVal Section As String, ByVal Key As String, ByVal Value As String) WritePrivateProfileString(Section, Key, Value, sFile) Flush() End Sub Public Sub WriteInteger(ByVal Section As String, ByVal Key As String, ByVal Value As Integer) WriteString(Section, Key, CStr(Value)) Flush() End Sub Public Sub WriteBoolean(ByVal Section As String, ByVal Key As String, ByVal Value As Boolean) WriteString(Section, Key, CStr(CInt(Value))) Flush() End Sub Private Sub Flush() FlushPrivateProfileString(0, 0, 0, sFile) End Sub |
within my code I use the following to write a to a [setion] key=value
1 2 | Dim objIniFile As New IniFile("your file path\yourfile.ini") objIniFile.WriteString("Section name", "Keyname", "your value as string") |
‘to read from a [section] key=value
1 2 | Dim objIniFile As New IniFile("your file path\yourfile.ini") Dim boolData As Boolean = objIniFile.GetBoolean("your Section name" "yourkey",True) |










































