我的C#版的INIFile 类
由于项目中需要一个处理ini文件的类,而.net的FrameWork中似乎并没有专门处理ini文件的类,网上大概搜索了一下,能够搜到几篇,大概有两种实现方式:一个是通过windows的api,包装一下而已,一个是通过正则表达式。但是他们提供的雷在功能上都无法满足要求,于是决定重新新一个。由于时间比较仓促,决定采用api,这样会简单一些。
该类具有以下功能:添加删除Section,添加、删除和修改键值,能够读写string, int, long, float, double, DateTime, 等类型的键值,能够判断某个Section 是否存在,判断某个键是否存在,能够返回该文件中所有的Section名称列表,创建和删除文件,判断文件是否存在。
下面是该类的代码
该类具有以下功能:添加删除Section,添加、删除和修改键值,能够读写string, int, long, float, double, DateTime, 等类型的键值,能够判断某个Section 是否存在,判断某个键是否存在,能够返回该文件中所有的Section名称列表,创建和删除文件,判断文件是否存在。
下面是该类的代码
1
using System;
2
using System.IO;
3
using System.Text;
4
using System.Configuration;
5
using System.Runtime.InteropServices;
6
using System.Collections.Specialized;
7
using System.Collections;
8
/**//// <summary>
9
/// Summary description for IniFile
10
/// </summary>
11
namespace PQSys
12

{
13
/**//// <summary>
14
/// 用于处理INI文件的类
15
/// </summary>
16
public class INIFile
17
{
18
string _FileName;
19
20
导入DLL函数#region 导入DLL函数
21
//[DllImport("kernel32.dll")]
22
// private extern static int GetPrivateProfileIntA(string segName, string keyName, int iDefault, string fileName);
23
[DllImport("kernel32.dll")]
24
private extern static int GetPrivateProfileStringA(string segName, string keyName, string sDefault, StringBuilder buffer, int nSize, string fileName);
25
[DllImport("kernel32.dll")]
26
private extern static int GetPrivateProfileSectionA(string segName, StringBuilder buffer, int nSize, string fileName);
27
[DllImport("kernel32.dll")]
28
private extern static int WritePrivateProfileSectionA(string segName, string sValue, string fileName);
29
[DllImport("kernel32.dll")]
30
private extern static int WritePrivateProfileStringA(string segName, string keyName, string sValue, string fileName);
31
[DllImport("kernel32.dll")]
32
private extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);
33
#endregion
34
35
public INIFile(string FileName)
36
{
37
_FileName = FileName;
38
if (!FileExists())
39
CreateFile();
40
}
41
AboutFile#region AboutFile
42
43
#endregion
44
Read#region Read
45
/**//// <summary>
46
/// 返回字符串
47
/// </summary>
48
public string ReadString(string Section, string Key)
49
{
50
StringBuilder buffer= new StringBuilder(65535);
51
GetPrivateProfileStringA(Section, Key, "", buffer, buffer.Capacity, _FileName);
52
return buffer.ToString();
53
}
54
/**//// <summary>
55
/// 返回int型的数
56
/// </summary>
57
public virtual int ReadInt(string Section, string Key)
58
{
59
int result;
60
try
61
{
62
result = int.Parse(this.ReadString(Section, Key));
63
}
64
catch
65
{
66
result = -1;
67
}
68
return result;
69
}
70
/**//// <summary>
71
/// 返回long型的数
72
/// </summary>
73
public virtual long ReadLong(string Section, string Key)
74
{
75
long result;
76
try
77
{
78
result = long.Parse(this.ReadString(Section, Key));
79
}
80
catch
81
{
82
result = -1;
83
}
84
return result;
85
}
86
/**//// <summary>
87
/// 返回byte型的数
88
/// </summary>
89
public virtual byte ReadByte(string Section, string Key)
90
{
91
byte result;
92
try
93
{
94
result = byte.Parse(this.ReadString(Section, Key));
95
}
96
catch
97
{
98
result = 0;
99
}
100
return result;
101
}
102
/**//// <summary>
103
/// 返回float型的数
104
/// </summary>
105
public virtual float ReadFloat(string Section, string Key)
106
{
107
float result;
108
try
109
{
110
result = float.Parse(this.ReadString(Section, Key));
111
}
112
catch
113
{
114
result = -1;
115
}
116
return result;
117
}
118
/**//// <summary>
119
/// 返回double型的数
120
/// </summary>
121
public virtual double ReadDouble(string Section, string Key)
122
{
123
double result;
124
try
125
{
126
result = double.Parse(this.ReadString(Section, Key));
127
}
128
catch
129
{
130
result = -1;
131
}
132
return result;
133
}
134
/**//// <summary>
135
/// 返回日期型的数
136
/// </summary>
137
public virtual DateTime ReadDateTime(string Section, string Key)
138
{
139
DateTime result;
140
try
141
{
142
result = DateTime.Parse(this.ReadString(Section, Key));
143
}
144
catch
145
{
146
result = DateTime.Parse("0-0-0"); ;
147
}
148
return result;
149
}
150
/**//// <summary>
151
/// 读bool量
152
/// </summary>
153
public virtual bool ReadBool(string Section, string Key)
154
{
155
bool result;
156
try
157
{
158
result = bool.Parse(this.ReadString(Section, Key));
159
}
160
catch
161
{
162
result = bool.Parse("0-0-0"); ;
163
}
164
return result;
165
}
166
#endregion _Endregion;
167
Write#region Write
168
/**//// <summary>
169
/// 用于写任何类型的键值到ini文件中
170
/// </summary>
171
/// <param name="Section">该键所在的节名称</param>
172
/// <param name="Key">该键的名称</param>
173
/// <param name="Value">该键的值</param>
174
public void Write(string Section, string Key, object Value)
175
{
176
if (Value!=null)
177
WritePrivateProfileStringA(Section, Key, Value.ToString(), _FileName);
178
else
179
WritePrivateProfileStringA(Section, Key, null, _FileName);
180
}
181
182
#endregion
183
others#region others
184
/**//// <summary>
185
/// 返回该配置文件中所有Section名称的集合
186
/// </summary>
187
public ArrayList ReadSections()
188
{
189
byte[] buffer = new byte[65535];
190
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), _FileName);
191
int iCnt, iPos;
192
ArrayList arrayList = new ArrayList();
193
string tmp;
194
if (rel>0)
195
{
196
iCnt = 0; iPos = 0;
197
for (iCnt = 0; iCnt < rel; iCnt++)
198
{
199
if (buffer[iCnt] == 0x00)
200
{
201
tmp =System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt).Trim();
202
iPos = iCnt + 1;
203
if (tmp != "")
204
arrayList.Add(tmp);
205
}
206
}
207
}
208
return arrayList;
209
}
210
/**//// <summary>
211
/// 判断指定的节是否存在
212
/// </summary>
213
public bool SectionExists(string Section)
214
{
215
//done SectionExists
216
StringBuilder buffer= new StringBuilder(65535);
217
GetPrivateProfileSectionA(Section, buffer, buffer.Capacity, _FileName);
218
if (buffer.ToString().Trim() == "")
219
return false;
220
else
221
return true;
222
}
223
/**//// <summary>
224
/// 判断指定的节中指定的键是否存在
225
/// </summary>
226
public bool ValueExits(string Section, string Key)
227
{
228
if (ReadString(Section, Key).Trim() == "")
229
return false;
230
else
231
return true;
232
}
233
/**//// <summary>
234
/// 删除指定的节中的指定键
235
/// </summary>
236
/// <param name="Section">该键所在的节的名称</param>
237
/// <param name="Key">该键的名称</param>
238
public void DeleteKey(string Section, string Key)
239
{
240
Write(Section, Key, null);
241
}
242
/**//// <summary>
243
/// 删除指定的节的所有内容
244
/// </summary>
245
/// <param name="Section">要删除的节的名字</param>
246
public void DeleteSection(string Section)
247
{
248
WritePrivateProfileSectionA(Section, null, _FileName);
249
}
250
/**//// <summary>
251
/// 添加一个节
252
/// </summary>
253
/// <param name="Section">要添加的节名称</param>
254
public void AddSection(string Section)
255
{
256
WritePrivateProfileSectionA(Section, "", _FileName);
257
}
258
#endregion
259
AboutFile#region AboutFile
260
/**//// <summary>
261
/// 删除文件
262
/// </summary>
263
public void DeleteFile()
264
{
265
if (FileExists())
266
File.Delete(_FileName);
267
}
268
/**//// <summary>
269
/// 创建文件
270
/// </summary>
271
public void CreateFile()
272
{
273
File.Create(_FileName).Close();
274
}
275
/**//// <summary>
276
/// 判断文件是否存在
277
/// </summary>
278
/// <returns></returns>
279
public bool FileExists()
280
{
281
return File.Exists(_FileName);
282
}
283
#endregion
284
}
285
}
286
287
using System;2
using System.IO;3
using System.Text;4
using System.Configuration;5
using System.Runtime.InteropServices;6
using System.Collections.Specialized;7
using System.Collections;8

/**//// <summary>9
/// Summary description for IniFile10
/// </summary>11
namespace PQSys12


{13

/**//// <summary>14
/// 用于处理INI文件的类15
/// </summary>16
public class INIFile17

{18
string _FileName; 19

20

导入DLL函数#region 导入DLL函数21
//[DllImport("kernel32.dll")]22
// private extern static int GetPrivateProfileIntA(string segName, string keyName, int iDefault, string fileName);23
[DllImport("kernel32.dll")]24
private extern static int GetPrivateProfileStringA(string segName, string keyName, string sDefault, StringBuilder buffer, int nSize, string fileName);25
[DllImport("kernel32.dll")]26
private extern static int GetPrivateProfileSectionA(string segName, StringBuilder buffer, int nSize, string fileName);27
[DllImport("kernel32.dll")]28
private extern static int WritePrivateProfileSectionA(string segName, string sValue, string fileName);29
[DllImport("kernel32.dll")]30
private extern static int WritePrivateProfileStringA(string segName, string keyName, string sValue, string fileName);31
[DllImport("kernel32.dll")]32
private extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);33
#endregion34

35
public INIFile(string FileName)36

{37
_FileName = FileName;38
if (!FileExists())39
CreateFile();40
}41

AboutFile#region AboutFile42

43
#endregion44

Read#region Read45

/**//// <summary>46
/// 返回字符串47
/// </summary>48
public string ReadString(string Section, string Key)49

{ 50
StringBuilder buffer= new StringBuilder(65535); 51
GetPrivateProfileStringA(Section, Key, "", buffer, buffer.Capacity, _FileName); 52
return buffer.ToString(); 53
}54

/**//// <summary>55
/// 返回int型的数56
/// </summary>57
public virtual int ReadInt(string Section, string Key)58

{59
int result;60
try61

{62
result = int.Parse(this.ReadString(Section, Key));63
}64
catch65

{66
result = -1;67
}68
return result;69
}70

/**//// <summary>71
/// 返回long型的数72
/// </summary>73
public virtual long ReadLong(string Section, string Key)74

{75
long result;76
try77

{78
result = long.Parse(this.ReadString(Section, Key));79
}80
catch81

{82
result = -1;83
}84
return result; 85
}86

/**//// <summary>87
/// 返回byte型的数88
/// </summary>89
public virtual byte ReadByte(string Section, string Key)90

{91
byte result;92
try93

{94
result = byte.Parse(this.ReadString(Section, Key));95
}96
catch97

{98
result = 0;99
}100
return result;101
}102

/**//// <summary>103
/// 返回float型的数104
/// </summary>105
public virtual float ReadFloat(string Section, string Key)106

{107
float result;108
try109

{110
result = float.Parse(this.ReadString(Section, Key));111
}112
catch113

{114
result = -1;115
}116
return result;117
}118

/**//// <summary>119
/// 返回double型的数120
/// </summary>121
public virtual double ReadDouble(string Section, string Key)122

{123
double result;124
try125

{126
result = double.Parse(this.ReadString(Section, Key));127
}128
catch129

{130
result = -1;131
}132
return result;133
}134

/**//// <summary>135
/// 返回日期型的数136
/// </summary>137
public virtual DateTime ReadDateTime(string Section, string Key)138

{139
DateTime result;140
try141

{142
result = DateTime.Parse(this.ReadString(Section, Key));143
}144
catch145

{146
result = DateTime.Parse("0-0-0"); ;147
}148
return result; 149
}150

/**//// <summary>151
/// 读bool量152
/// </summary>153
public virtual bool ReadBool(string Section, string Key)154

{155
bool result;156
try157

{158
result = bool.Parse(this.ReadString(Section, Key));159
}160
catch161

{162
result = bool.Parse("0-0-0"); ;163
}164
return result; 165
} 166
#endregion _Endregion;167

Write#region Write168

/**//// <summary>169
/// 用于写任何类型的键值到ini文件中170
/// </summary>171
/// <param name="Section">该键所在的节名称</param>172
/// <param name="Key">该键的名称</param>173
/// <param name="Value">该键的值</param>174
public void Write(string Section, string Key, object Value)175

{176
if (Value!=null) 177
WritePrivateProfileStringA(Section, Key, Value.ToString(), _FileName);178
else179
WritePrivateProfileStringA(Section, Key, null, _FileName);180
}181

182
#endregion183

others#region others184

/**//// <summary>185
/// 返回该配置文件中所有Section名称的集合186
/// </summary>187
public ArrayList ReadSections()188

{189
byte[] buffer = new byte[65535];190
int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), _FileName);191
int iCnt, iPos;192
ArrayList arrayList = new ArrayList();193
string tmp;194
if (rel>0)195

{196
iCnt = 0; iPos = 0; 197
for (iCnt = 0; iCnt < rel; iCnt++)198

{199
if (buffer[iCnt] == 0x00)200

{201
tmp =System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt).Trim();202
iPos = iCnt + 1;203
if (tmp != "")204
arrayList.Add(tmp);205
}206
}207
}208
return arrayList;209
}210

/**//// <summary>211
/// 判断指定的节是否存在212
/// </summary>213
public bool SectionExists(string Section)214

{215
//done SectionExists216
StringBuilder buffer= new StringBuilder(65535);217
GetPrivateProfileSectionA(Section, buffer, buffer.Capacity, _FileName);218
if (buffer.ToString().Trim() == "")219
return false;220
else221
return true;222
}223

/**//// <summary>224
/// 判断指定的节中指定的键是否存在225
/// </summary>226
public bool ValueExits(string Section, string Key)227

{ 228
if (ReadString(Section, Key).Trim() == "")229
return false;230
else231
return true;232
}233

/**//// <summary>234
/// 删除指定的节中的指定键235
/// </summary>236
/// <param name="Section">该键所在的节的名称</param>237
/// <param name="Key">该键的名称</param>238
public void DeleteKey(string Section, string Key)239

{ 240
Write(Section, Key, null);241
}242

/**//// <summary>243
/// 删除指定的节的所有内容244
/// </summary>245
/// <param name="Section">要删除的节的名字</param>246
public void DeleteSection(string Section)247

{ 248
WritePrivateProfileSectionA(Section, null, _FileName);249
}250

/**//// <summary>251
/// 添加一个节252
/// </summary>253
/// <param name="Section">要添加的节名称</param>254
public void AddSection(string Section)255

{256
WritePrivateProfileSectionA(Section, "", _FileName);257
}258
#endregion259

AboutFile#region AboutFile260

/**//// <summary>261
/// 删除文件262
/// </summary>263
public void DeleteFile()264

{265
if (FileExists())266
File.Delete(_FileName);267
}268

/**//// <summary>269
/// 创建文件270
/// </summary>271
public void CreateFile()272

{273
File.Create(_FileName).Close();274
}275

/**//// <summary>276
/// 判断文件是否存在277
/// </summary>278
/// <returns></returns>279
public bool FileExists()280

{281
return File.Exists(_FileName);282
}283
#endregion 284
}285
}286

287


浙公网安备 33010602011771号