Imports System.IO
Public Class INIReader
Public ReadOnly file As String
Sub New(ByVal inifile As String)
If IO.File.Exists(inifile) Then
file = inifile
Else
'If our file isn't found, we want to let the user know.
Throw New FileNotFoundException("File does not exist.", inifile)
End If
End Sub
Public Function GetValue(ByVal value As String, Optional ByVal section As String = Nothing) As String
Using reader As New IO.StreamReader(file)
Dim temp As String
If section Is Nothing Then
While Not reader.EndOfStream
temp = reader.ReadLine().Trim
'If the line is null then we don't need to read it
If temp Is Nothing Then Continue While
'If the line is blank then we don't need to read it
If temp.Length = 0 Then Continue While
'If the line is a comment then we don't need to read it
If temp(0) = ";"c Then Continue While
'If the line is a section, then we don't need to read it
If temp(0) = "["c Then Continue While
'If the line begins with the property name, then we return everything after the "=".
If temp.Substring(0, temp.IndexOf("="c)).Trim = value Then
Return temp.Substring(temp.IndexOf("="c) + 1).Trim
End If
End While
'Either we got to the next section or the end of the file and nothing was found.
Return Nothing
Else
While Not reader.EndOfStream
temp = reader.ReadLine.Trim
'If the line is null then we don't need to read it
If temp Is Nothing Then Continue While
'If the line is blank then we don't need to read it
If temp.Length = 0 Then Continue While
'If the line is a comment then we don't need to read it
If temp(0) = ";"c Then Continue While
If temp.Substring(0, section.Length + 2) = "[" & section & "]" Then
'We've found the section, we need to clear the temp variable to use it for our property now
temp = Nothing
While Not reader.EndOfStream
temp = reader.ReadLine().Trim
'If the line is null then we don't need to read it
If temp Is Nothing Then Continue While
'If the line is blank then we don't need to read it
If temp.Length = 0 Then Continue While
'If the line is a comment then we don't need to read it
If temp(0) = ";"c Then Continue While
'If the line is a section, then the section we were looking for didn't have our property, so we exit.
If temp(0) = "["c Then Exit While
'If the line begins with the property name, then we return everything after the "=".
If temp.Substring(0, temp.IndexOf("="c)).Trim = value Then
Return temp.Substring(temp.IndexOf("="c) + 1).Trim
End If
End While
End If
End While
'Either we got to the next section or the end of the file and nothing was found.
Return Nothing
End If
temp = Nothing
reader.Close()
End Using
End Function
Public Function GetProperties(ByVal section As String) As List(Of String())
GetProperties = New List(Of String())
Using reader As New IO.StreamReader(file)
Dim temp As String
While Not reader.EndOfStream
temp = reader.ReadLine.Trim
'If the line is null then we don't need to read it
If temp Is Nothing Then Continue While
'If the line is blank then we don't need to read it
If temp.Length = 0 Then Continue While
'If the line is a comment then we don't need to read it
If temp(0) = ";"c Then Continue While
'If the string isn't even as long as "[" & section & "]" it's obviously not the section
If temp.Length < section.Length + 2 Then Continue While
If temp.Substring(0, section.Length + 2) = "[" & section & "]" Then
'We've found the section, we need to clear the temp variable to use it for our property now
temp = Nothing
While Not reader.EndOfStream
temp = reader.ReadLine().Trim
'If the line is null then we don't need to read it
If temp Is Nothing Then Continue While
'If the line is blank then we don't need to read it
If temp.Length = 0 Then Continue While
'If the line is a comment then we don't need to read it
If temp(0) = ";"c Then Continue While
'If the line is a section, then the section we were looking for didn't have our property, so we exit.
If temp(0) = "["c Then Exit While
'If the line begins with the property name, then we return everything after the "=".
If temp.Substring(0, temp.IndexOf("="c)).Trim.Length <> 0 Then
GetProperties.Add({temp.Substring(0, temp.IndexOf("="c)).Trim, temp.Substring(temp.IndexOf("="c) + 1).Trim})
End If
End While
End If
End While
'Either we got to the next section or the end of the file and nothing was found.
Return GetProperties
temp = Nothing
reader.Close()
End Using
End Function
End Class
Public Class INISection
Public properties As List(Of String())
Sub New(ByVal reader As INIReader, ByVal section As String)
properties = reader.GetProperties(section)
End Sub
Public Function GetValue(ByVal name As String)
Return properties.Find(Function(x) x(0) = name)(1)
End Function
End Class