-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiniWrapper.vb
More file actions
221 lines (181 loc) · 8.43 KB
/
iniWrapper.vb
File metadata and controls
221 lines (181 loc) · 8.43 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'2026-4-10
Imports System.IO
Imports IniParser
Imports IniParser.Model
Imports IniParser.Parser
Public Class iniWrapper
Public iniDict As Model.IniData
Private iniParserObj As New FileIniDataParser
Private _iniPath As String
Public ReadOnly EXEfolder As String = IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Public Sub New(Optional iniPath As String = "[EXE directory]", Optional AutoGenerateIfMissing As Boolean = False)
Dim iniName As String
iniParserObj.Parser.Configuration.SkipInvalidLines = True
If iniPath = "[EXE directory]" Then
iniPath = EXEfolder & "\" & "UserOptions.ini"
Else
If iniPath.Contains("\") = False Then
iniName = iniPath
iniPath = EXEfolder & "\" & iniName
ElseIf iniPath.Substring(0, 2) <> "\\" And iniPath.Substring(1, 2) <> ":\" And iniPath.Substring(0, 1) = "\" Then
iniPath = EXEfolder & iniPath
End If
End If
_iniPath = iniPath
If IO.File.Exists(iniPath) = True Then 'global vars
iniDict = iniParserObj.ReadFile(iniPath) 'global vars
Else
If AutoGenerateIfMissing = True OrElse vbYes = MsgBox("Settings ini file was not found. Generate a new one?", MsgBoxStyle.YesNo) Then
GenerateNewINIfile(iniPath)
Else
End
End If
End If
End Sub
Public Function GenerateNewINIfile(iniPath As String) As Boolean
'running this will also overwrite the existing file and reset settings to default
If File.Exists(iniPath) = True Then
Return False
End If
Dim defaultIni As String = Path.GetDirectoryName(iniPath) & "\defaults.dat"
If File.Exists(defaultIni) = False Then
Throw New FileNotFoundException("default ini config definition file is missing.")
End If
File.Copy(defaultIni, iniPath, True)
iniDict = iniParserObj.ReadFile(iniPath) 'global vars
Return True
End Function
Public Property Entry(keyName As String, Optional section As String = "Main") As String
Get
If iniDict(section).ContainsKey(keyName) = False Then
'MsgBox(EXEfolder)
Throw New Collections.Generic.KeyNotFoundException("Given key does not exist in ini file." & vbCrLf & "Section: " & section & ", Key: " & keyName)
End If
Dim keyVal As String = iniDict(section)(keyName)
Return keyVal
End Get
Set(keyValue As String)
If iniDict(section).ContainsKey(keyName) = False Then
Throw New Collections.Generic.KeyNotFoundException("Given key does not exist in ini file." & vbCrLf & "Section: " & section & ", Key: " & keyName)
End If
iniDict(section)(keyName) = keyValue
iniParserObj.WriteFile(_iniPath, iniDict)
End Set
End Property
Public Function RemoveEntry(keyName As String, Optional section As String = "Main", Optional byINIval As String = "") As Boolean
If iniDict(section).ContainsKey(keyName) = False Then
Throw New Collections.Generic.KeyNotFoundException("Given key does not exist in ini file." & vbCrLf & "Section: " & section & ", Key: " & keyName)
End If
iniDict(section).RemoveKey(keyName)
SaveFile()
Return True
End Function
Public Function RemoveEntryByVal(val As String, Optional section As String = "Main") As Boolean
Dim secData As SectionData = iniDict.Sections.GetSectionData(section)
For Each key As IniParser.Model.KeyData In secData.Keys
If key.Value = val Then
iniDict(section).RemoveKey(key.KeyName)
SaveFile()
Return True
End If
Next
Return False
End Function
Public Sub ResetKeyPairList(prefix As String, section As String, valList As List(Of String))
Dim secData As SectionData = iniDict.Sections.GetSectionData(section)
Dim keyArr() As IniParser.Model.KeyData = secData.Keys.ToArray
For i As Integer = 0 To UBound(keyArr)
If keyArr(i).KeyName.StartsWith(prefix) = True Then
iniDict(section).RemoveKey(keyArr(i).KeyName)
End If
Next
For i As Integer = valList.Count - 1 To 0 Step -1
Dim keyName As String = $"{prefix}_{i}"
iniDict(section)(keyName) = valList(i)
Next
SaveFile()
End Sub
Public ReadOnly Property Contains(keyName As String, Optional section As String = "Main") As Boolean
Get
Return iniDict(section).ContainsKey(keyName)
End Get
End Property
Public Sub AddIterativeEntry(keyPrefix As String, val As String, Optional section As String = "Main")
If iniDict.Sections.ContainsSection(section) = False Then
Throw New Collections.Generic.KeyNotFoundException("Given section does not exist in ini file." & vbCrLf & "Section: " & section)
End If
Dim i As Integer = 0
Dim keyName As String = $"{keyPrefix}_0"
Do While Contains(keyName, section) = True
i += 1
keyName = $"{keyPrefix}_{i}"
Loop
iniDict(section)(keyName) = val
'iniDict(section).GetKeyData(keyName).Comments = New List(Of String)({"comment"})
SaveFile()
End Sub
Public Function GetIterativeEntryArray(keyPrefix As String, Optional section As String = "Main") As String()
Dim valList As New List(Of String), i As Integer = 0
Dim keyName As String = $"{keyPrefix}_0"
Do While Contains(keyName, section)
valList.Add(Entry(keyName, section))
i += 1
keyName = $"{keyPrefix}_{i}"
Loop
Return valList.ToArray
End Function
Public Sub SaveFile()
'Note this should only be called after editing the iniDict directly
iniParserObj.WriteFile(_iniPath, iniDict)
End Sub
Public Sub OpenINI(Optional OpenFolder As Boolean = False, Optional CloseFirst As Boolean = False)
If OpenFolder = False Then
OpenTextFile(_iniPath,, True, "CloseIfOpen")
Else
Process.Start("explorer.exe", IO.Path.GetDirectoryName(_iniPath))
End If
End Sub
Private Function OpenTextFile(FilePath As String, Optional ByRef ExceptionStr As String = "", Optional ShowErrorMessageWindow As Boolean = True, Optional CloseMode As String = "[Off,CloseIfOpen,ForceClose]") As Boolean
'2025-3-15
If CloseMode = "[Off,CloseIfOpen,ForceClose]" Then
CloseMode = "Off"
ElseIf CloseMode <> "CloseIfOpen" And CloseMode <> "ForceClose" And CloseMode <> "Off" Then
Throw New Exception
End If
Try
FilePath = Strings.Split(FilePath, vbCrLf)(0)
If IO.File.Exists(FilePath) = False Then
Throw New IO.FileNotFoundException("File not found." & vbCrLf & FilePath)
End If
If CloseMode <> "Off" Then
For Each proc As Process In Process.GetProcessesByName("notepad")
If Not String.IsNullOrEmpty(proc.MainWindowTitle) AndAlso proc.MainWindowTitle.Contains(Path.GetFileName(FilePath)) Then
Try
proc.CloseMainWindow() ' Attempt graceful closure
proc.WaitForExit(1000)
If CloseMode = "ForceClose" AndAlso proc.HasExited = False Then
proc.Kill() ' Force close if still running
Else
'
End If
Catch ex As Exception
ExceptionStr = ex.Message
If ShowErrorMessageWindow = True Then
MsgBox("Error closing Notepad:" & vbCrLf & ExceptionStr)
End If
Return False
End Try
End If
Next
End If
Process.Start("notepad.exe", FilePath)
Return True
Catch ex As Exception
ExceptionStr = ex.Message
If ShowErrorMessageWindow = True Then
MsgBox("Error opening text file:" & vbCrLf & ExceptionStr)
End If
Return False
End Try
End Function
End Class