Public Class IniFile
Private strFilename As String
' Constructor, accepting a filename
Public Sub New(ByVal Filename As String)
strFilename = Filename
End Sub
' Read-only filename property
ReadOnly Property FileName() As String
Get
Return strFilename
End Get
End Property
Public Function GetString(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As String) As String
If FileName IsNot Nothing Then
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = API.GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then
Return Left(objResult.ToString, intCharCount)
Else
Return String.Empty
End If
Else
' Returns a string from your win.INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = API.GetProfileString(Section, Key, _
[Default], objResult, objResult.Capacity)
If intCharCount > 0 Then
Return Left(objResult.ToString, intCharCount)
Else
Return String.Empty
End If
End If
End Function
Public Function GetInteger(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Integer) As Integer
If FileName IsNot Nothing Then
' Returns an integer from your INI file
Return API.GetPrivateProfileInt(Section, Key, _
[Default], strFilename)
Else
' Returns an integer from your win.INI file
Return API.GetProfileInt(Section, Key, _
[Default])
End If
End Function
Public Function GetBoolean(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Boolean) As Boolean
If FileName IsNot Nothing Then
' Returns a boolean from your INI file
Return CBool(API.GetPrivateProfileInt(Section, Key, _
CBool([Default]), strFilename))
Else
' Returns a boolean from your win.INI file
Return CBool(API.GetProfileInt(Section, Key, _
CBool([Default])))
End If
End Function
Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
If FileName IsNot Nothing Then
' Writes a string to your INI file
API.WritePrivateProfileString(Section, Key, Value, strFilename)
Else
' Writes a string to your win.INI file
API.WriteProfileString(Section, Key, Value)
End If
Flush()
End Sub
Public Sub WriteInteger(ByVal Section As String, _
ByVal Key As String, ByVal Value As Integer)
' Writes an integer to your INI file
WriteString(Section, Key, CStr(Value))
End Sub
Public Sub WriteBoolean(ByVal Section As String, _
ByVal Key As String, ByVal Value As Boolean)
' Writes a boolean to your INI file
WriteString(Section, Key, CStr(CInt(Value)))
End Sub
Private Sub Flush()
' Stores all the cached changes to your INI file
API.FlushPrivateProfileString(0, 0, 0, strFilename)
End Sub
End Class
Public Class API
' API functions
Public 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
Public 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
Public 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
Public 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
Public Declare Function WriteProfileString Lib "kernel32" _
Alias "WriteProfileStringA" (ByVal lpszSection As String, _
ByVal lpszKeyName As String, ByVal lpszString As String) _
As Integer
Public Declare Function GetProfileString Lib "kernel32" Alias _
"GetProfileStringA" (ByVal lpAppName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer) As Integer
Public Declare Function GetProfileInt Lib "kernel32" _
Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal _
lpKeyName As String, ByVal nDefault As Integer) As Integer
End Class