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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
| Imports System.Text
Imports System.Runtime.InteropServices
Public Class IniFile
Const KEY_BUFFER_SIZE As Integer = 2047 'размер буфера для значения ключа
Const SECTION_BUFFER_SIZE As Integer = 16384 'размер буфера для считівания секций
Dim _fileName As String
#Region "Свойства"
Public ReadOnly Property FileName() As String
Get
Return _fileName
End Get
End Property
#End Region
#Region "Публичные методы"
Public Sub New(ByVal FileName As String)
FileName = Replace(FileName, Application.StartupPath + "\", "")
_fileName = Application.StartupPath + "\" + FileName
End Sub
'считываем названия всех секций
Public Function ReadSections() As String()
'получаем строку секций с разделителем vbNullChar секций
Dim strSections As String = InternalReadString(Nothing, Nothing, "", EIniAtoms.Section)
If strSections = "" Then Return New String(-1) {}
'получаем массив секций (с разделителями через Chr(0))
Return strSections.ToString.Split(vbNullChar)
End Function
'считывание названий ключей заданной секции
Public Function ReadSection(ByVal Section As String) As String()
'получаем строку ключей с разделителем vbNullChar ключей
Dim strKeys As String = InternalReadString(Section, Nothing, "", EIniAtoms.Section)
If strKeys = "" Then Return New String(-1) {}
'получаем массив ключей (с разделителями через Chr(0))
Return strKeys.ToString.Split(vbNullChar)
End Function
'получение значений всех ключей для заданной секции в формате Ключ=значение
Public Function ReadSectionValues(ByVal Section As String) As Hashtable
Dim KeyValueHash As New Hashtable
'получаем все ключи секции
Dim keys() As String = ReadSection(Section)
'создаем хэш-таблицу пар Ключ=значение
For Each key As String In keys
'для каждого ключа
KeyValueHash(key) = InternalReadString(Section, key, "", EIniAtoms.Key)
Next
Return KeyValueHash
End Function
Public Function ReadString(ByVal Section As String, ByVal Key As String) As String
Return ReadString(Section, Key, "")
End Function
Public Function ReadString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String) As String
Return InternalReadString(Section, Key, [Default], EIniAtoms.Key)
End Function
Public Function ReadInt32(ByVal Section As String, ByVal Key As String) As Integer
Return Convert.ToInt32(ReadString(Section, Key))
End Function
Public Function ReadInt32(ByVal Section As String, ByVal Key As String, ByVal [Default] As Integer) As Integer
Return Convert.ToInt32(ReadString(Section, Key, Convert.ToString([Default])))
End Function
Public Function ReadInt64(ByVal Section As String, ByVal Key As String) As Int64
Return Convert.ToInt64(ReadString(Section, Key))
End Function
Public Function ReadDouble(ByVal Section As String, ByVal Key As String) As Double
Return Convert.ToDouble(ReadString(Section, Key))
End Function
Public Function ReadByte(ByVal Section As String, ByVal Key As String) As Byte
Return Convert.ToByte(ReadString(Section, Key))
End Function
Public Function ReadDate(ByVal Section As String, ByVal Key As String) As Date
Return Convert.ToDateTime(ReadString(Section, Key))
End Function
Public Function ReadBool(ByVal Section As String, ByVal Key As String) As Boolean
Return Convert.ToBoolean(ReadString(Section, Key))
End Function
Public Function ReadBool(ByVal Section As String, ByVal Key As String, ByVal [Default] As Boolean) As Boolean
Return Convert.ToBoolean(ReadString(Section, Key, Convert.ToString([Default])))
End Function
'записывает значения секции по ключу в ReturnedValue
'возвращает true, если было что-то прочитано
Public Function ReadString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String, ByRef ReturnedValue$) As Boolean
Return InternalReadString(Section, Key, [Default], ReturnedValue, EIniAtoms.Key)
End Function
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As String)
If NativeMethods.WritePrivateProfileString(Section, Key, Value, _fileName) = 0 Then
GenerateError()
End If
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Int32)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Int64)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Double)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Byte)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Char)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Boolean)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Date)
Write(Section, Key, Convert.ToString(Value))
End Sub
Public Sub Write(ByVal Section As String, ByVal Key As String, ByVal Value As Object)
Write(Section, Key, Convert.ToString(Value))
End Sub
'запись значений , которые хранятся в формате Ключ=значение в хэш-таблице
Public Sub Write(ByVal Section As String, ByVal Values As Hashtable)
For Each de As DictionaryEntry In Values
Write(Section, de.Key, de.Value)
Next
End Sub
'удаление секции
Public Sub EraseSection(ByVal Section As String)
If NativeMethods.WritePrivateProfileString(Section, Nothing, Nothing, Me._fileName) = 0 Then
GenerateError()
End If
End Sub
'удаление ключа
Public Sub DeleteKey(ByVal Section As String, ByVal Key As String)
If NativeMethods.WritePrivateProfileString(Section, Key, Nothing, Me._fileName) = 0 Then
GenerateError()
End If
End Sub
#End Region
#Region "Приватные методы"
Private Enum EIniAtoms
Key
Section
End Enum
Private Function InternalReadString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String, ByRef ReturnedValue As String, ByVal IniAtom As EIniAtoms) As Boolean
ReturnedValue = New String(vbNullChar, SECTION_BUFFER_SIZE)
Dim readed As Int32 = NativeMethods.GetPrivateProfileString(Section, Key, [Default], ReturnedValue, GetBufferSize(IniAtom), Me._fileName)
If readed > 0 Then
If ReturnedValue.Chars(readed - 1) = Nothing Then
ReturnedValue = ReturnedValue.Substring(0, readed - 1)
Else
ReturnedValue = ReturnedValue.Substring(0, readed)
End If
Return True
Else
Return False
End If
End Function
Private Function InternalReadString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String, ByVal IniAtom As EIniAtoms) As String
Dim ret As String = String.Empty
If InternalReadString(Section, Key, [Default], ret, IniAtom) Then
Return ret
Else
Return ""
End If
End Function
Private Function GetBufferSize(ByVal IniAtom As EIniAtoms) As Integer
Select Case IniAtom
Case EIniAtoms.Key
Return KEY_BUFFER_SIZE
Case EIniAtoms.Section
Return SECTION_BUFFER_SIZE
Case Else
Return -1
End Select
End Function
Private Sub GenerateError()
Throw New Exception(String.Format("Невозможно выполнить оперцию! Внутренний код ошибки [{0}]", NativeMethods.GetLastError))
End Sub
#End Region
End Class
Friend NotInheritable Class NativeMethods
Private Sub New()
End Sub
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetPrivateProfileSection( _
ByVal lpAppName As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetPrivateProfileSectionNames( _
ByVal lpszReturnBuffer As String, _
ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetPrivateProfileString( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetPrivateProfileString( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As IntPtr, _
ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetPrivateProfileString( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetPrivateProfileStruct( _
ByVal lpszSection As String, _
ByVal lpszKey As String, _
ByVal lpStruct As IntPtr, _
ByVal uSizeStruct As Int32, _
ByVal szFile As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function WritePrivateProfileSection( _
ByVal lpAppName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function WritePrivateProfileString( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function WritePrivateProfileStruct( _
ByVal lpszSection As String, _
ByVal lpszKey As String, _
ByVal lpStruct As IntPtr, _
ByVal uSizeStruct As Int32, _
ByVal szFile As String) As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Function GetLastError() As Int32
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Friend Shared Sub SetLastError(ByVal err As Int32)
End Sub
End Class |