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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| Public Class Inifile
#Region "Notes"
'
' Class: Inifile.vb
' Author: Ivan Lutrov <ivan@lutrov.com>
' Version: 1.3
' Description: Lightweight infile handling class
' Written: September 2006.
' Notes:
'
' THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
' MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE
' AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2.1 OF
' THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
'
' THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
' WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
' MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
' LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
'
' YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC
' LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO:
' THE FREE SOFTWARE FOUNDATION, INC,
' 59 TEMPLE PLACE, SUITE 330, BOSTON, MA 02111-1307 USA
'
'
#End Region
#Region "API declarations"
Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal DefVal As String, ByVal RetVal As StringBuilder, ByVal Size As Integer, ByVal FileName As String) As Integer
Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal Value As String, ByVal FileName As String) As Integer
Private Declare Ansi Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" (ByVal AppName As String, ByVal KeyName As String, ByVal DefVal As Integer, ByVal FileName As String) As Integer
Private Declare Ansi Function FlushPrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal AppName As Integer, ByVal KeyName As Integer, ByVal Value As Integer, ByVal FileName As String) As Integer
#End Region
#Region "Constructor"
Public Sub New(Optional ByVal FileName As String = "")
If Len(FileName) > 0 Then
MyFileName = FileName
Else
MyFileName = Replace(Application.ExecutablePath(), ".exe", ".ini")
End If
End Sub
#End Region
#Region "Private properties"
Private MyFileName As String
ReadOnly Property FileName() As String
Get
Return MyFileName
End Get
End Property
#End Region
#Region "Public methods"
Public Function LoadString(ByVal Section As String, ByVal Key As String, Optional ByVal DefaultValue As String = "") As String
Try
Dim Result As New StringBuilder(256), Length As Integer
Length = GetPrivateProfileString(Section, Key, DefaultValue, Result, Result.Capacity, MyFileName)
If Length > 0 Then
Return Left(Result.ToString, Length)
End If
Catch Ex As Exception
Me.ErrorHandler(Ex, "Inifile.LoadString")
End Try
Return Nothing
End Function
Public Function LoadInteger(ByVal Section As String, ByVal Key As String, Optional ByVal DefaultValue As Integer = 0) As Integer
Try
Return GetPrivateProfileInt(Section, Key, DefaultValue, MyFileName)
Catch Ex As Exception
Me.ErrorHandler(Ex, "Inifile.LoadInteger")
End Try
End Function
Public Function LoadBoolean(ByVal Section As String, ByVal Key As String, Optional ByVal DefaultValue As Boolean = False) As Boolean
Try
Return IIf(GetPrivateProfileInt(Section, Key, CInt(DefaultValue), MyFileName) = 1, True, False)
Catch Ex As Exception
Me.ErrorHandler(Ex, "Inifile.LoadBoolean")
End Try
End Function
Public Sub SaveString(ByVal Section As String, ByVal Key As String, ByVal Value As String)
Try
WritePrivateProfileString(Section, Key, Value, MyFileName)
FlushPrivateProfileString(0, 0, 0, MyFileName)
Catch Ex As Exception
Me.ErrorHandler(Ex, "Inifile.SaveString")
End Try
End Sub
Public Sub SaveInteger(ByVal Section As String, ByVal Key As String, ByVal Value As Integer)
Try
WritePrivateProfileString(Section, Key, CStr(Value), MyFileName)
FlushPrivateProfileString(0, 0, 0, MyFileName)
Catch Ex As Exception
Me.ErrorHandler(Ex, "Inifile.SaveInteger")
End Try
End Sub
Public Sub SaveBoolean(ByVal Section As String, ByVal Key As String, ByVal Value As Boolean)
Try
WritePrivateProfileString(Section, Key, CStr(Value), MyFileName)
FlushPrivateProfileString(0, 0, 0, MyFileName)
Catch Ex As Exception
Me.ErrorHandler(Ex, "Inifile.SaveBoolean")
End Try
End Sub
Private Sub ErrorHandler(ByVal Ex As Exception, ByVal Where As String)
MessageBox.Show("An error has occured in " + Where + "():" + vbCrLf + vbCrLf + Ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub
#End Region
End Class |
Partager