📄 inifile.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "INIFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2005 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib 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 Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: INIFile
'
''
' Provides methods for manipulating an INI initialization file.
'
' @remarks An INI file contains sections in a format like<br><br>
' <i>
' [section]<br>
' key=value<br><br>
' </i>
' A new INIFile object can be created using the <b>NewINIFile</b> method.
' <pre>
' Dim ini As INIFile
' Set ini = NewINIFile("c:\settings.ini")
' </pre>
' <p>If the file name does not specify the directory, then the file will
' be created in the Windows directory. In order to specify the current
' directory, preceed the file name with a current directory symbol ".".
' <pre>
' Dim ini As INIFile
' Set ini = NewINIFile(".\local.ini")
' </pre>
' </p>
'
' @see INISectionWriter
'
Option Explicit
Implements IObject
Private mFileName As String
Private mAutoFlush As Boolean
''
' Returns the auto-flush state of the object.
'
' @Return Returns True if AutoFlush is active, False otherwise.
' @remarks When AutoFlush is set to True, the object will call Flush
' after any method that writes to the INI file.
'
Public Property Get AutoFlush() As Boolean
AutoFlush = mAutoFlush
End Property
''
' Sets the auto-flush state of the object.
'
' @param RHS The new auto-flush state of the object.
' @remarks When AutoFlush is set to True, the object will call Flush
' after any method that writes to the INI file.
'
Public Property Let AutoFlush(ByVal RHS As Boolean)
mAutoFlush = RHS
End Property
''
' Flushes any cached updates to the INI file.
'
Public Sub Flush()
Call WritePrivateProfileString(vbNullString, vbNullString, vbNullString, vbNullString)
End Sub
''
' Returns the name of the INI file being manipulated.
'
' @return The INI file name.
'
Public Property Get FileName() As String
FileName = mFileName
End Property
''
' Sets a key-value pair within a section of an INI file.
'
' @param Section The INI section within the file to set the value.
' @param Key The name of the value to be set.
' @param Value The value to be set in the INI file.
' @remarks If an object is passed in, then it must implement the <b>cObject</b>
' interface or an exception will be thrown. All other datatypes will be converted
' to their normal string value.
'
Public Sub SetValue(ByVal Section As String, ByVal Key As String, ByRef Value As Variant)
If Len(Section) = 0 Then _
Throw Cor.NewArgumentException("Section cannot be an empty string.", "Section")
If Len(Key) = 0 Then _
Throw Cor.NewArgumentException("Key cannot be an empty string.", "Key")
Dim s As String
s = Convert.ToString(Value)
' We don't want a null string, since that would
' delete the INI entry instead.
If cString.IsNull(s) Then s = ""
If WritePrivateProfileString(Section, Key, s, mFileName) = BOOL_FALSE Then IOError Err.LastDllError
If mAutoFlush Then Call Flush
End Sub
''
' Returns the string value of the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section.
' <p>An empty string is a valid value in an INI file. Testing for an empty string
' does not mean the value was not found.</p>
'
Public Function GetString(ByVal Section As String, ByVal Key As String, Optional ByVal Default As String) As String
If Len(Section) = 0 Then _
Throw Cor.NewArgumentException("Section cannot be an empty string.", "Section")
If Len(Key) = 0 Then _
Throw Cor.NewArgumentException("Key cannot be an empty string.", "Key")
' MSDN says we cannot pass in a null value for the default.
If cString.IsNull(Default) Then Default = ""
Dim Size As Long
Dim Buf As String
Size = 512
Do
Size = Size * 2
Buf = String$(Size, vbNullChar)
Size = GetPrivateProfileString(Section, Key, Default, Buf, Size, mFileName)
Loop While Size = Len(Buf) - 1
GetString = Left$(Buf, Size)
End Function
''
' Returns a Long value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Long.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Long using the CLng function. If an error happens, then the default is returned.
'
Public Function GetLong(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Long) As Long
GetLong = ConvertTo(vbLong, GetString(Section, Key), Default)
End Function
''
' Returns a Integer value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to an Integer.
' @remarks An INI file contains all values as Strings. The value is converted back
' into an Integer using the CInt function. If an error happens, then the default is returned.
'
Public Function GetInteger(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Integer) As Integer
GetInteger = ConvertTo(vbInteger, GetString(Section, Key), Default)
End Function
''
' Returns a Byte value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Byte.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Byte using the CByte function. If an error happens, then the default is returned.
'
Public Function GetByte(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Byte) As Byte
GetByte = ConvertTo(vbByte, GetString(Section, Key), Default)
End Function
''
' Returns a Boolean value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Boolean.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Boolean using the CBool function. If an error happens, then the default is returned.
'
Public Function GetBoolean(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Boolean) As Boolean
GetBoolean = ConvertTo(vbBoolean, GetString(Section, Key), Default)
End Function
''
' Returns a Date value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Date.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Date using the CDate function. If an error happens, then the default is returned.
'
Public Function GetDate(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Date) As Date
GetDate = ConvertTo(vbDate, GetString(Section, Key), Default)
End Function
''
' Returns a Double value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Double.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Double using the CDbl function. If an error happens, then the default is returned.
'
Public Function GetDouble(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Double) As Double
GetDouble = ConvertTo(vbDouble, GetString(Section, Key), Default)
End Function
''
' Returns a Single value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Single.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Single using the CSng function. If an error happens, then the default is returned.
'
Public Function GetSingle(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Single) As Single
GetSingle = ConvertTo(vbSingle, GetString(Section, Key), Default)
End Function
''
' Returns a Currency value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
' @param Key The key in the section of an INI file to retrieve the value of.
' @param Default The default value to return if the key is not found in the section, or
' the value could not be converted to a Currency.
' @remarks An INI file contains all values as Strings. The value is converted back
' into a Currency using the CCur function. If an error happens, then the default is returned.
'
Public Function GetCurrency(ByVal Section As String, ByVal Key As String, Optional ByVal Default As Currency) As Currency
GetCurrency = ConvertTo(vbCurrency, GetString(Section, Key), Default)
End Function
''
' Returns a Decimal value from the specified key in the specified section of an INI file.
'
' @param Section The section within the INI file to search for the key.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -