-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniHelper.vb
More file actions
185 lines (163 loc) · 6.89 KB
/
Copy pathIniHelper.vb
File metadata and controls
185 lines (163 loc) · 6.89 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
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
''' <summary>
''' Helper class for reading and writing INI files using Windows API
''' </summary>
Public Class IniHelper
Private ReadOnly _filePath As String
' Windows API declarations for INI file operations
<DllImport("kernel32", CharSet:=CharSet.Unicode)>
Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
End Function
<DllImport("kernel32", CharSet:=CharSet.Unicode)>
Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
End Function
''' <summary>
''' Initialize the INI helper with the specified file path
''' </summary>
''' <param name="filePath">Full path to the INI file</param>
Public Sub New(filePath As String)
_filePath = filePath
' Ensure the directory exists
Dim directoryPath As String = Path.GetDirectoryName(_filePath)
If Not String.IsNullOrEmpty(directoryPath) AndAlso Not Directory.Exists(directoryPath) Then
Directory.CreateDirectory(directoryPath)
End If
End Sub
''' <summary>
''' Read a string value from the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
''' <param name="defaultValue">Default value if key not found</param>
''' <returns>The value from the INI file or default value</returns>
Public Function ReadValue(section As String, key As String, defaultValue As String) As String
Dim temp As New StringBuilder(255)
Dim result As Integer = GetPrivateProfileString(section, key, defaultValue, temp, 255, _filePath)
Return temp.ToString()
End Function
''' <summary>
''' Read an integer value from the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
''' <param name="defaultValue">Default value if key not found</param>
''' <returns>The integer value from the INI file or default value</returns>
Public Function ReadInteger(section As String, key As String, defaultValue As Integer) As Integer
Dim value As String = ReadValue(section, key, defaultValue.ToString())
Dim result As Integer
If Integer.TryParse(value, result) Then
Return result
Else
Return defaultValue
End If
End Function
''' <summary>
''' Read a boolean value from the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
''' <param name="defaultValue">Default value if key not found</param>
''' <returns>The boolean value from the INI file or default value</returns>
Public Function ReadBoolean(section As String, key As String, defaultValue As Boolean) As Boolean
Dim value As String = ReadValue(section, key, defaultValue.ToString())
Dim result As Boolean
If Boolean.TryParse(value, result) Then
Return result
Else
Return defaultValue
End If
End Function
''' <summary>
''' Write a string value to the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
''' <param name="value">Value to write</param>
Public Sub WriteValue(section As String, key As String, value As String)
WritePrivateProfileString(section, key, value, _filePath)
End Sub
''' <summary>
''' Write an integer value to the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
''' <param name="value">Value to write</param>
Public Sub WriteInteger(section As String, key As String, value As Integer)
WriteValue(section, key, value.ToString())
End Sub
''' <summary>
''' Write a boolean value to the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
''' <param name="value">Value to write</param>
Public Sub WriteBoolean(section As String, key As String, value As Boolean)
WriteValue(section, key, value.ToString())
End Sub
''' <summary>
''' Delete a key from the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="key">Key name</param>
Public Sub DeleteKey(section As String, key As String)
WritePrivateProfileString(section, key, Nothing, _filePath)
End Sub
''' <summary>
''' Delete an entire section from the INI file
''' </summary>
''' <param name="section">Section name</param>
Public Sub DeleteSection(section As String)
WritePrivateProfileString(section, Nothing, Nothing, _filePath)
End Sub
''' <summary>
''' Check if the INI file exists
''' </summary>
''' <returns>True if file exists, False otherwise</returns>
Public Function FileExists() As Boolean
Return File.Exists(_filePath)
End Function
''' <summary>
''' Get the full path of the INI file
''' </summary>
''' <returns>Full file path</returns>
Public ReadOnly Property FilePath As String
Get
Return _filePath
End Get
End Property
''' <summary>
''' Read a list of string values from the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="keyPrefix">Prefix for the keys (e.g., "Table" for Table0, Table1, etc.)</param>
''' <param name="count">Number of items to read</param>
''' <returns>List of string values</returns>
Public Function ReadList(section As String, keyPrefix As String, count As Integer) As List(Of String)
Dim result As New List(Of String)
For i As Integer = 0 To count - 1
Dim value As String = ReadValue(section, $"{keyPrefix}{i}", "")
If Not String.IsNullOrEmpty(value) Then
result.Add(value)
End If
Next
Return result
End Function
''' <summary>
''' Write a list of string values to the INI file
''' </summary>
''' <param name="section">Section name</param>
''' <param name="keyPrefix">Prefix for the keys (e.g., "Table" for Table0, Table1, etc.)</param>
''' <param name="values">List of values to write</param>
Public Sub WriteList(section As String, keyPrefix As String, values As List(Of String))
' Clear existing entries by deleting the section first
DeleteSection(section)
' Write count
WriteInteger(section, "Count", values.Count)
' Write each value
For i As Integer = 0 To values.Count - 1
WriteValue(section, $"{keyPrefix}{i}", values(i))
Next
End Sub
End Class