using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引入命名空间
using System.Collections.Specialized;
using System.IO;
8:
9:
namespace ConfigFiles
11: {
/// <summary>
/// IniFiles的类
/// </summary>
class IniFiles
16: {
//INI文件名
18:
//声明读写INI文件的API函数
)]
string filePath);
)]
string filePath);
24:
//类的构造函数,传递INI文件名
string AFileName)
27: {
// 判断文件是否存在
new FileInfo(AFileName);
30:
//Todo:搞清枚举的用法
if ((!fileInfo.Exists))
//|| (FileAttributes.Directory in fileInfo.Attributes))
//文件不存在,建立文件
false, System.Text.Encoding.Default);
try
37: {
);
39: sw.Close();
40: }
41:
catch
43: {
));
45: }
46: }
47:
//必须是完全路径,不能是相对路径
49: FileName = fileInfo.FullName;
50: }
51:
//写INI文件
string Value)
54: {
if (!WritePrivateProfileString(Section, Ident, Value, FileName))
56: {
));
58: }
59: }
60:
//读取INI文件指定
string Default)
63: {
new Byte[65535];
int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
//必须设定0(系统默认的代码页)的编码方式,否则无法支持中文
string s = Encoding.GetEncoding(0).GetString(Buffer);
68: s = s.Substring(0, bufLen);
return s.Trim();
70: }
71:
//读整数
int Default)
74: {
string intStr = ReadString(Section, Ident, Convert.ToString(Default));
try
77: {
return Convert.ToInt32(intStr);
79:
80: }
catch (Exception ex)
82: {
83: Console.WriteLine(ex.Message);
return Default;
85: }
86: }
87:
//写整数
int Value)
90: {
91: WriteString(Section, Ident, Value.ToString());
92: }
93:
//读布尔
bool Default)
96: {
try
98: {
return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
100: }
catch (Exception ex)
102: {
103: Console.WriteLine(ex.Message);
return Default;
105: }
106: }
107:
//写Bool
bool Value)
110: {
111: WriteString(Section, Ident, Convert.ToString(Value));
112: }
113:
//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
string Section, StringCollection Idents)
116: {
new Byte[16384];
//Idents.Clear();
119:
null, Buffer, Buffer.GetUpperBound(0),
121: FileName);
//对Section进行解析
123: GetStringsFromBuffer(Buffer, bufLen, Idents);
124: }
125:
int bufLen, StringCollection Strings)
127: {
128: Strings.Clear();
if (bufLen != 0)
130: {
int start = 0;
int i = 0; i < bufLen; i++)
133: {
if ((Buffer[i] == 0) && ((i - start) > 0))
135: {
136: String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
137: Strings.Add(s);
138: start = i + 1;
139: }
140: }
141: }
142: }
143:
//从Ini文件中,读取所有的Sections的名称
void ReadSections(StringCollection SectionList)
146: {
//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
byte[65535];
int bufLen = 0;
null, Buffer,
151: Buffer.GetUpperBound(0), FileName);
152: GetStringsFromBuffer(Buffer, bufLen, SectionList);
153: }
154:
//读取指定的Section的所有Value到列表中
string Section, NameValueCollection Values)
157: {
new StringCollection();
159: ReadSection(Section, KeyList);
160: Values.Clear();
in KeyList)
162: {
));
164:
165: }
166: }
167:
168:
//清除某个Section
string Section)
171: {
//
null, FileName))
174: {
175:
));
177: }
178: }
179:
//删除某个Section下的键
string Ident)
182: {
null, FileName);
184: }
185:
//Note:对于Win9X,来说需要实现UpdateFile方法将缓冲中的数据写入文件
//在Win NT, 2000和XP上,都是直接写文件,没有缓冲,所以,无须实现UpdateFile
//执行完对Ini文件的修改之后,应该调用本方法更新缓冲区。
void UpdateFile()
190: {
null, FileName);
192: }
193:
//检查某个Section下的某个键值是否存在
string Ident)
196: {
//
new StringCollection();
199: ReadSection(Section, Idents);
return Idents.IndexOf(Ident) > -1;
201: }
202:
//确保资源的释放
204: ~IniFiles()
205: {
206: UpdateFile();
207: }
208: }
209: }