INIControl.cs
7.16 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
/*----------------------------------------------------------------
// 文 件 名:INIControl.cs
// 功能描述:INI文件读写
//
// 创建标识:彭玉柱 2010-07-12
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.Globalization;
namespace GeneralLib
{
public class INIControl
{
private static INIControl iniColtrol;
private static string filePath;//INI文件路径
//声明读写INI文件的API函数
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
/// <summary>
/// 构造函数
/// </summary>
private INIControl()
{
}
/// <summary>
/// 得到INIControl对象
/// </summary>
/// <param name="filePath">INI文件路径,包括文件名</param>
/// <returns>INIControl</returns>
public static INIControl GetInstances(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
//判断INI文件是否存在
if (!fileInfo.Exists)
{
throw (new ApplicationException("Ini文件不存在"));
}
INIControl.filePath = fileInfo.FullName;
return iniColtrol == null ? iniColtrol = new INIControl() : iniColtrol;
}
/// <summary>
/// 读取所有的段落(Section)的名称
/// </summary>
/// <param name="SectionList">StringCollection</param>
public void ReadSections(StringCollection SectionList)
{
//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
byte[] buffer = new byte[65535];
int bufLen = 0;
bufLen = GetPrivateProfileString(null, null, null, buffer, buffer.GetUpperBound(0), filePath);
GetStringsFromBuffer(buffer, bufLen, SectionList);
}
/// <summary>
/// 字节数组转字符串集合
/// </summary>
/// <param name="buffer"></param>
/// <param name="bufLen"></param>
/// <param name="Strings"></param>
private void GetStringsFromBuffer(Byte[] buffer, int bufLen, StringCollection Strings)
{
Strings.Clear();
if (bufLen != 0)
{
int start = 0;
for (int i = 0; i < bufLen; i++)
{
if ((buffer[i] == 0) && ((i - start) > 0))
{
String s = Encoding.GetEncoding(0).GetString(buffer, start, i - start);
Strings.Add(s);
start = i + 1;
}
}
}
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">关键字</param>
/// <param name="val">值</param>
public void WriteValue(string section, string key, object val)
{
try
{
WritePrivateProfileString(section, key, val.ToString(), filePath);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex, true);
}
}
/// <summary>
/// 读字符串
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">关键字</param>
/// <param name="defaultVal">无法读取时默认的值</param>
/// <returns>值</returns>
public string ReadString(string section, string key, string defaultVal)
{
Byte[] buffer = new Byte[65535];
int bufLen = GetPrivateProfileString(section, key, defaultVal, buffer, buffer.GetUpperBound(0), filePath);
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
string s = Encoding.GetEncoding(0).GetString(buffer);
s = s.Substring(0, bufLen);
return s.Trim().TrimEnd('\0');//杨斌 2016-07-06
}
/// <summary>
/// 读整数
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">关键字</param>
/// <param name="defaultVal">无法读取时默认的值</param>
/// <returns>值</returns>
public int ReadInt(string section, string key, int defaultVal)
{
string val = this.ReadString(section, key, defaultVal.ToString());
try
{
return Convert.ToInt16(val);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
return defaultVal;
}
}
/// <summary>
/// 读short值
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">关键字</param>
/// <param name="defaultVal">无法读取时默认的值</param>
/// <returns>值</returns>
public short ReadShort(string section, string key, short defaultVal)
{
string val = this.ReadString(section, key, defaultVal.ToString());
try
{
return short.Parse(val);
}
catch (System.Exception ex)
{
SystemLog.WriterLog(ex);
return defaultVal;
}
}
/// <summary>
/// 读bool值
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">关键字</param>
/// <param name="defaultVal">无法读取时默认的值</param>
/// <returns>值</returns>
public bool ReadBool(string section, string key, bool defaultVal)
{
string val = this.ReadString(section, key, defaultVal.ToString());
try
{
return Convert.ToBoolean(val.ToLower());
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
return defaultVal;
}
}
/// <summary>
/// 读double值
/// </summary>
/// <param name="section">段落名</param>
/// <param name="key">关键字</param>
/// <param name="defaultVal">无法读取时默认的值</param>
/// <returns>值</returns>
public double ReadDouble(string section, string key, double defaultVal)
{
string val = this.ReadString(section, key, defaultVal.ToString());
val = ConvertOper.DoNumberString(val);//杨斌 2013-08-29
try
{
return Convert.ToDouble(val);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
return defaultVal;
}
}
}
}