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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| Imports System.Text.RegularExpressions
Imports System.IO
Public Class IniFile
Private ReadOnly regexSection As Regex = New Regex("\s*\[([\w\s]+)\]", RegexOptions.Compiled)
Private ReadOnly regexKeyValuePair As Regex = New Regex("\s*(\w+)=(.*)", RegexOptions.Compiled)
Public FileName_ As String
Public Sections As Dictionary(Of String, Dictionary(Of String, String))
Public Sub New()
Try
Sections = New Dictionary(Of String, Dictionary(Of String, String))()
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Sub
Public Sub New(ByVal fileName As String)
Try
FileName_ = fileName
Reload()
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Sub
Default Public Property ConvertedIndexer(ByVal section As String, ByVal key As String) As String
Get
Return GetValue(section, key)
End Get
Set(ByVal value As String)
SetValue(section, key, value)
End Set
End Property
Public Sub Reload()
Try
If FileName_ = Nothing Then
Throw New InvalidOperationException("The file name is not defined")
End If
Sections = New Dictionary(Of String, Dictionary(Of String, String))()
Using rd As StreamReader = New StreamReader(FileName_)
Dim curSection As String = ""
Dim line As String
Dim m As Match
line = rd.ReadLine()
While (line = rd.ReadLine()) <> Nothing
If line.Trim().StartsWith(";") Then
Continue While
Else
m = regexSection.Match(line)
If m.Success Then
curSection = m.Groups(1).Value
Else
m = regexKeyValuePair.Match(line)
If m.Success Then
Dim key As String = m.Groups(1).Value
Dim value As String = m.Groups(2).Value
Dim dSection As Dictionary(Of String, String) = Nothing
If Not Sections.TryGetValue(curSection, dSection) Then
dSection = AddSection(curSection)
End If
dSection.Add(key, value)
End If
End If
End If
End While
End Using
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Sub
Public Function AddSection(ByVal name As String) As Dictionary(Of String, String)
Try
Dim section As Dictionary(Of String, String) = New Dictionary(Of String, String)()
Sections.Add(name, section)
Return section
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Function
Public Function GetValue(ByVal section As String, ByVal key As String) As String
Try
If section = Nothing Then
section = ""
End If
Dim dSection As Dictionary(Of String, String) = Nothing
If Sections.TryGetValue(section, dSection) Then
Dim value As String = Nothing
If dSection.TryGetValue(key, value) Then
Return value
Else
Return Nothing
End If
Else
Return Nothing
End If
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Function
Public Sub SetValue(ByVal section As String, ByVal key As String, ByVal value As String)
Try
If section = Nothing Then
section = ""
End If
Dim dSection As Dictionary(Of String, String) = Nothing
If Not Sections.TryGetValue(section, dSection) Then
dSection = AddSection(section)
End If
dSection(key) = value
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Sub
Public Sub Save()
Try
If FileName_ = Nothing Then
Throw New InvalidOperationException("The file name is not defined")
End If
Using wr As StreamWriter = New StreamWriter(FileName_)
Dim noSection As Dictionary(Of String, String) = Nothing
If Sections.TryGetValue("", noSection) Then
For Each key As String In noSection.Keys
wr.WriteLine("{0}={1}", key, Sections("")(key))
Next
wr.WriteLine()
End If
For Each secName As String In Sections.Keys
If secName.Length = 0 Then
Continue For
End If
wr.WriteLine("[{0}]", secName)
For Each key As String In Sections(secName).Keys
wr.WriteLine("{0}={1}", key, Sections(secName)(key))
Next
wr.WriteLine()
Next
End Using
Catch ex As Exception
MsgBox(ex.Message)
Throw
End Try
End Sub
End Class |
Partager