Try this class...
Public Class IniFile<br />
' API functions<br />
Private Declare Ansi Function GetPrivateProfileString _<br />
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _<br />
(ByVal lpApplicationName As String, _<br />
ByVal lpKeyName As String, ByVal lpDefault As String, _<br />
ByVal lpReturnedString As System.Text.StringBuilder, _<br />
ByVal nSize As Integer, ByVal lpFileName As String) _<br />
As Integer<br />
Private Declare Ansi Function WritePrivateProfileString _<br />
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _<br />
(ByVal lpApplicationName As String, _<br />
ByVal lpKeyName As String, ByVal lpString As String, _<br />
ByVal lpFileName As String) As Integer<br />
Private Declare Ansi Function GetPrivateProfileInt _<br />
Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _<br />
(ByVal lpApplicationName As String, _<br />
ByVal lpKeyName As String, ByVal nDefault As Integer, _<br />
ByVal lpFileName As String) As Integer<br />
Private Declare Ansi Function FlushPrivateProfileString _<br />
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _<br />
(ByVal lpApplicationName As Integer, _<br />
ByVal lpKeyName As Integer, ByVal lpString As Integer, _<br />
ByVal lpFileName As String) As Integer<br />
<br />
Dim strFilename As String<br />
<br />
' Constructor, accepting a filename<br />
Public Sub New(ByVal Filename As String)<br />
strFilename = Filename<br />
End Sub<br />
<br />
' Read-only filename property<br />
ReadOnly Property FileName() As String<br />
Get<br />
Return strFilename<br />
End Get<br />
End Property<br />
<br />
Public Function GetString(ByVal Section As String, _<br />
ByVal Key As String, ByVal [Default] As String) As String<br />
' Returns a string from your INI file<br />
Dim intCharCount As Integer<br />
Dim objResult As New System.Text.StringBuilder(256)<br />
intCharCount = GetPrivateProfileString(Section, Key, _<br />
[Default], objResult, objResult.Capacity, strFilename)<br />
If intCharCount > 0 Then GetString = _<br />
Left(objResult.ToString, intCharCount)<br />
End Function<br />
<br />
Public Function GetInteger(ByVal Section As String, _<br />
ByVal Key As String, ByVal [Default] As Integer) As Integer<br />
' Returns an integer from your INI file<br />
Return GetPrivateProfileInt(Section, Key, _<br />
[Default], strFilename)<br />
End Function<br />
<br />
Public Function GetBoolean(ByVal Section As String, _<br />
ByVal Key As String, ByVal [Default] As Boolean) As Boolean<br />
' Returns a boolean from your INI file<br />
Return (GetPrivateProfileInt(Section, Key, _<br />
CInt([Default]), strFilename) = 1)<br />
End Function<br />
<br />
Public Sub WriteString(ByVal Section As String, _<br />
ByVal Key As String, ByVal Value As String)<br />
' Writes a string to your INI file<br />
WritePrivateProfileString(Section, Key, Value, strFilename)<br />
Flush()<br />
End Sub<br />
<br />
Public Sub WriteInteger(ByVal Section As String, _<br />
ByVal Key As String, ByVal Value As Integer)<br />
' Writes an integer to your INI file<br />
WriteString(Section, Key, CStr(Value))<br />
Flush()<br />
End Sub<br />
<br />
Public Sub WriteBoolean(ByVal Section As String, _<br />
ByVal Key As String, ByVal Value As Boolean)<br />
' Writes a boolean to your INI file<br />
WriteString(Section, Key, CStr(CInt(Value)))<br />
Flush()<br />
End Sub<br />
<br />
Private Sub Flush()<br />
' Stores all the cached changes to your INI file<br />
FlushPrivateProfileString(0, 0, 0, strFilename)<br />
End Sub<br />
<br />
End Class
You would then use it like so...
Dim objIniFile As New IniFile("c:\data.ini")<br />
objIniFile.WriteString("Settings", "ClockTime", "12:59")<br />
Dim strData As String = _<br />
objIniFile.GetString("Settings", "ClockTime", "(none)")
Hope this helps
Steve Jowett
|