' ' IniReader class Imports System Imports System.Text Imports System.Collections Imports System.Runtime.InteropServices Imports Microsoft.VisualBasic ''' ''' The INIReader class can read keys from and write keys to an INI file. ''' ''' ''' This class uses several Win32 API functions to read from and write to INI files. It will not work on Linux or FreeBSD. ''' Public Class IniReader ''' ''' The GetPrivateProfileInt function retrieves an integer associated with a key in the specified section of an initialization file. ''' ''' Pointer to a null-terminated string specifying the name of the section in the initialization file. ''' Pointer to the null-terminated string specifying the name of the key whose value is to be retrieved. This value is in the form of a string; the GetPrivateProfileInt function converts the string into an integer and returns the integer. ''' Specifies the default value to return if the key name cannot be found in the initialization file. ''' Pointer to a null-terminated string that specifies the name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory. ''' The return value is the integer equivalent of the string following the specified key name in the specified initialization file. If the key is not found, the return value is the specified default value. If the value of the key is less than zero, the return value is zero. 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 ''' ''' The WritePrivateProfileString function copies a string into the specified section of an initialization file. ''' ''' Pointer to a null-terminated string containing the name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case-independent; the string can be any combination of uppercase and lowercase letters. ''' Pointer to the null-terminated string containing the name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted. ''' Pointer to a null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpKeyName parameter is deleted. ''' Pointer to a null-terminated string that specifies the name of the initialization file. ''' If the function successfully copies the string to the initialization file, the return value is nonzero; if the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is zero. 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 ''' ''' The GetPrivateProfileString function retrieves a string from the specified section in an initialization file. ''' ''' Pointer to a null-terminated string that specifies the name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer. ''' Pointer to the null-terminated string specifying the name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter. ''' Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL.
Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.
''' Pointer to the buffer that receives the retrieved string. ''' Specifies the size, in TCHARs, of the buffer pointed to by the lpReturnedString parameter. ''' Pointer to a null-terminated string that specifies the name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory. ''' The return value is the number of characters copied to the buffer, not including the terminating null character. 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 StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer ''' ''' The GetPrivateProfileSectionNames function retrieves the names of all sections in an initialization file. ''' ''' Pointer to a buffer that receives the section names associated with the named file. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character. ''' Specifies the size, in TCHARs, of the buffer pointed to by the lpszReturnBuffer parameter. ''' Pointer to a null-terminated string that specifies the name of the initialization file. If this parameter is NULL, the function searches the Win.ini file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory. ''' The return value specifies the number of characters copied to the specified buffer, not including the terminating null character. If the buffer is not large enough to contain all the section names associated with the specified initialization file, the return value is equal to the length specified by nSize minus two. Private Declare Ansi Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer() As Byte, ByVal nSize As Integer, ByVal lpFileName As String) As Integer ''' ''' The WritePrivateProfileSection function replaces the keys and values for the specified section in an initialization file. ''' ''' Pointer to a null-terminated string specifying the name of the section in which data is written. This section name is typically the name of the calling application. ''' Pointer to a buffer containing the new key names and associated values that are to be written to the named section. ''' Pointer to a null-terminated string containing the name of the initialization file. If this parameter does not contain a full path for the file, the function searches the Windows directory for the file. If the file does not exist and lpFileName does not contain a full path, the function creates the file in the Windows directory. The function does not create a file if lpFileName contains the full path and file name of a file that does not exist. ''' If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Private Declare Ansi Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer ''' Constructs a new IniReader instance. ''' Specifies the full path to the INI file (the file doesn't have to exist). Public Sub New(ByVal file As String) Filename = file End Sub ''' Gets or sets the full path to the INI file. ''' A String representing the full path to the INI file. ''' Public Property Filename() As String Get Return m_Filename End Get Set(ByVal Value As String) m_Filename = Value End Set End Property ''' Gets or sets the section you're working in. (aka 'the active section') ''' A String representing the section you're working in. ''' Public Property Section() As String Get Return m_Section End Get Set(ByVal Value As String) m_Section = Value End Set End Property ''' Reads an Integer from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' The value to return if the specified key isn't found. ''' Returns the value of the specified section/key pair, or returns the default value if the specified section/key pair isn't found in the INI file. Public Function ReadInteger(ByVal section As String, ByVal key As String, ByVal defVal As Integer) As Integer Return GetPrivateProfileInt(section, key, defVal, Filename) End Function ''' Reads an Integer from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' Returns the value of the specified section/key pair, or returns 0 if the specified section/key pair isn't found in the INI file. Public Function ReadInteger(ByVal section As String, ByVal key As String) As Integer Return ReadInteger(section, key, 0) End Function ''' Reads an Integer from the specified key of the active section. ''' The key from which to return the value. ''' The section to search in. ''' Returns the value of the specified Key, or returns the default value if the specified Key isn't found in the active section of the INI file. Public Function ReadInteger(ByVal key As String, ByVal defVal As Integer) As Integer Return ReadInteger(Section, key, defVal) End Function ''' Reads an Integer from the specified key of the active section. ''' The key from which to return the value. ''' Returns the value of the specified key, or returns 0 if the specified key isn't found in the active section of the INI file. Public Function ReadInteger(ByVal key As String) As Integer Return ReadInteger(key, 0) End Function ''' Reads a String from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' The value to return if the specified key isn't found. ''' Returns the value of the specified section/key pair, or returns the default value if the specified section/key pair isn't found in the INI file. Public Function ReadString(ByVal section As String, ByVal key As String, ByVal defVal As String) As String Dim sb As New StringBuilder(MAX_ENTRY) Dim Ret As Integer = GetPrivateProfileString(section, key, defVal, sb, MAX_ENTRY, Filename) Return sb.ToString() End Function ''' Reads a String from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' Returns the value of the specified section/key pair, or returns an empty String if the specified section/key pair isn't found in the INI file. Public Function ReadString(ByVal section As String, ByVal key As String) As String Return ReadString(section, key, "") End Function ''' Reads a String from the specified key of the active section. ''' The key from which to return the value. ''' Returns the value of the specified key, or returns an empty String if the specified key isn't found in the active section of the INI file. Public Function ReadString(ByVal key As String) As String Return ReadString(Section, key) End Function ''' Reads a Long from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' The value to return if the specified key isn't found. ''' Returns the value of the specified section/key pair, or returns the default value if the specified section/key pair isn't found in the INI file. Public Function ReadLong(ByVal section As String, ByVal key As String, ByVal defVal As Long) As Long Return Long.Parse(ReadString(section, key, defVal.ToString())) End Function ''' Reads a Long from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' Returns the value of the specified section/key pair, or returns 0 if the specified section/key pair isn't found in the INI file. Public Function ReadLong(ByVal section As String, ByVal key As String) As Long Return ReadLong(section, key, 0) End Function ''' Reads a Long from the specified key of the active section. ''' The key from which to return the value. ''' The section to search in. ''' Returns the value of the specified key, or returns the default value if the specified key isn't found in the active section of the INI file. Public Function ReadLong(ByVal key As String, ByVal defVal As Long) As Long Return ReadLong(Section, key, defVal) End Function ''' Reads a Long from the specified key of the active section. ''' The key from which to return the value. ''' Returns the value of the specified Key, or returns 0 if the specified Key isn't found in the active section of the INI file. Public Function ReadLong(ByVal key As String) As Long Return ReadLong(key, 0) End Function ''' Reads a Byte array from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' Returns the value of the specified section/key pair, or returns null (Nothing in VB.NET) if the specified section/key pair isn't found in the INI file. Public Function ReadByteArray(ByVal section As String, ByVal key As String) As Byte() Try Return Convert.FromBase64String(ReadString(section, key)) Catch End Try Return Nothing End Function ''' Reads a Byte array from the specified key of the active section. ''' The key from which to return the value. ''' Returns the value of the specified key, or returns null (Nothing in VB.NET) if the specified key pair isn't found in the active section of the INI file. Public Function ReadByteArray(ByVal key As String) As Byte() Return ReadByteArray(Section, key) End Function ''' Reads a Boolean from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' The value to return if the specified key isn't found. ''' Returns the value of the specified section/key pair, or returns the default value if the specified section/key pair isn't found in the INI file. Public Function ReadBoolean(ByVal section As String, ByVal key As String, ByVal defVal As Boolean) As Boolean Dim b_ret As Boolean = False Try b_ret = Boolean.Parse(ReadString(section, key, defVal.ToString())) Catch b_ret = False End Try Return b_ret End Function ''' Reads a Boolean from the specified key of the specified section. ''' The section to search in. ''' The key from which to return the value. ''' Returns the value of the specified section/key pair, or returns false if the specified section/key pair isn't found in the INI file. Public Function ReadBoolean(ByVal section As String, ByVal key As String) As Boolean Return ReadBoolean(section, key, False) End Function ''' Reads a Boolean from the specified key of the specified section. ''' The key from which to return the value. ''' The value to return if the specified key isn't found. ''' Returns the value of the specified key pair, or returns the default value if the specified key isn't found in the active section of the INI file. Public Function ReadBoolean(ByVal key As String, ByVal defVal As Boolean) As Boolean Return ReadBoolean(Section, key, defVal) End Function ''' Reads a Boolean from the specified key of the specified section. ''' The key from which to return the value. ''' Returns the value of the specified key, or returns false if the specified key isn't found in the active section of the INI file. Public Function ReadBoolean(ByVal key As String) As Boolean Return ReadBoolean(Section, key) End Function ''' Writes an Integer to the specified key in the specified section. ''' The section to write in. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal section As String, ByVal key As String, ByVal value As Integer) As Boolean Return Write(section, key, value.ToString()) End Function ''' Writes an Integer to the specified key in the active section. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal key As String, ByVal value As Integer) As Boolean Return Write(Section, key, value) End Function ''' Writes a String to the specified key in the specified section. ''' Specifies the section to write in. ''' Specifies the key to write to. ''' Specifies the value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal section As String, ByVal key As String, ByVal value As String) As Boolean Return (WritePrivateProfileString(section, key, value, Filename) <> 0) End Function ''' Writes a String to the specified key in the active section. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal key As String, ByVal value As String) As Boolean Return Write(Section, key, value) End Function ''' Writes a Long to the specified key in the specified section. ''' The section to write in. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal section As String, ByVal key As String, ByVal value As Long) As Boolean Return Write(section, key, value.ToString()) End Function ''' Writes a Long to the specified key in the active section. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal key As String, ByVal value As Long) As Boolean Return Write(Section, key, value) End Function ''' Writes a Byte array to the specified key in the specified section. ''' The section to write in. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal section As String, ByVal key As String, ByVal value() As Byte) As Boolean If value Is Nothing Then Return Write(section, key, CType(Nothing, String)) Else Return Write(section, key, value, 0, value.Length) End If End Function ''' Writes a Byte array to the specified key in the active section. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal key As String, ByVal value() As Byte) As Boolean Return Write(Section, key, value) End Function ''' Writes a Byte array to the specified key in the specified section. ''' The section to write in. ''' The key to write to. ''' The value to write. ''' An offset in value. ''' The number of elements of value to convert. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal section As String, ByVal key As String, ByVal value() As Byte, ByVal offset As Integer, ByVal length As Integer) As Boolean If value Is Nothing Then Return Write(section, key, CType(Nothing, String)) Else Return Write(section, key, Convert.ToBase64String(value, offset, length)) End If End Function ''' Writes a Boolean to the specified key in the specified section. ''' The section to write in. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal section As String, ByVal key As String, ByVal value As Boolean) As Boolean Return Write(section, key, value.ToString()) End Function ''' Writes a Boolean to the specified key in the active section. ''' The key to write to. ''' The value to write. ''' Returns true if the function succeeds, false otherwise. Public Function Write(ByVal key As String, ByVal value As Boolean) As Boolean Return Write(Section, key, value) End Function ''' Deletes a key from the specified section. ''' The section to delete from. ''' The key to delete. ''' Returns true if the function succeeds, false otherwise. Public Function DeleteKey(ByVal section As String, ByVal key As String) As Boolean Return (WritePrivateProfileString(section, key, Nothing, Filename) <> 0) End Function ''' Deletes a key from the active section. ''' The key to delete. ''' Returns true if the function succeeds, false otherwise. ''' Public Function DeleteKey(ByVal key As String) As Boolean Return (WritePrivateProfileString(Section, key, Nothing, Filename) <> 0) End Function ''' Deletes a section from an INI file. ''' The section to delete. ''' Returns true if the function succeeds, false otherwise. Public Function DeleteSection(ByVal section As String) As Boolean Return WritePrivateProfileSection(section, Nothing, Filename) <> 0 End Function ''' ''' Retrieves a list of all available sections in the INI file. ''' ''' ''' Returns an ArrayList with all available sections. ''' Public Function GetSectionNames() As ArrayList Try Dim buffer(MAX_ENTRY) As Byte GetPrivateProfileSectionNames(buffer, MAX_ENTRY, Filename) Dim parts() As String = Encoding.ASCII.GetString(buffer).Trim(ControlChars.NullChar).Split(ControlChars.NullChar) Return New ArrayList(parts) Catch End Try Return Nothing End Function 'Private variables and constants ''' ''' Holds the full path to the INI file. ''' Private m_Filename As String ''' ''' Holds the active section name ''' Private m_Section As String ''' ''' The maximum number of bytes in a section buffer. ''' Private Const MAX_ENTRY As Integer = 32768 End Class