C# 编写 WinCE 下操作 INI 文件类库
自己留存,不做解释,如果需要可以复制代码进行学习或者使用。有三个下载链接,前两个使用的是List和HashTable集合的方式,不支持注释和空行,而另一个使用的则是ArrayList集合的方式,支持注释和空行,但由于过多的拆箱和装箱操作,所以相对于List集合的方式比较耗费资源。如果下载使用建议选择HashTable方式,简单粗暴。Microsoft Visual Studio 2008 版本,.NET Framework 2.0 下通过编译,不喜欢勿喷,可以讨论,谢谢!
List源文件下载
HashTable源文件下载
ArrayList源文下载
Field.cs
/*
* 字段类
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IniFiles
{
public class Field
{
public string Name { get; set; } //字段名称
public string Value { get; set; } //字段信息
}
}
Section.cs
/*
* 节点类
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace IniFiles
{
public class Section
{
public string Name { get; set; } //节点名称
public List<Field> Field { get; set; } //字段列表
/// <summary>
/// 构造函数
/// </summary>
public Section()
{
Field = new List<Field>();
}
}
}
IniFiles.cs
/*
* IniFiles 配置文件 WinCE 类库 v1.0 版
* 作者:派克鼠
* QQ:53793494
* E-Mail:53793494@qq.com
* 完成时间:2020年6月23日
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace IniFiles
{
public class IniFiles
{
private string strFilename = null; // 配置文件名称
private List<Section> sections = new List<Section>(); // 配置信息
/// <summary>
/// 构造函数
/// </summary>
/// <param name="strFilename">文件名称</param>
public IniFiles(string strFilename)
{
this.strFilename = strFilename;
if (!File.Exists(strFilename))
{
try
{
FileStream fs = new FileStream(this.strFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Close();
}
catch { }
}
LoadFile();
}
/// <summary>
/// 销毁函数
/// </summary>
~IniFiles()
{
}
/// <summary>
/// 加载配置信息
/// </summary>
private void LoadFile()
{
bool blSection = false; // 是否为节点
string strTemp = null; // 临时数据
if (sections.Count > 0) { sections.Clear(); }
try
{
StreamReader sr = new StreamReader(strFilename, Encoding.Default); // 创建文件流用于读取配置文件
while (!sr.EndOfStream) // 循环至流的底部
{
// 如果临时数据不为空,则读取一行,一般情况下只在首次读取数据,或者上一次读取到数据为空,亦或者上一次读取数据为注释语句时才会读取数据
if (string.IsNullOrEmpty(strTemp)) { strTemp = sr.ReadLine(); }
if (strTemp.IndexOf('[') == 0 && strTemp.LastIndexOf(']') == strTemp.Length - 1) // 确认是否是节点
{
blSection = true; // 标记本节点开始
Section section = new Section(); // 创建节点
section.Name = strTemp.Substring(strTemp.IndexOf('[') + 1, strTemp.LastIndexOf(']') - 1); // 写入节点
while (!sr.EndOfStream && blSection) // 节点内循环
{
strTemp = sr.ReadLine(); // 读取一行临时数据
if (strTemp.IndexOf('=') > 0) // 确认是否为有效字段
{
Field field = new Field(); // 创建字段
field.Name = strTemp.Substring(0, strTemp.IndexOf('=')); // 写入字段名称
field.Value = strTemp.Substring(strTemp.IndexOf('=') + 1, strTemp.Length - strTemp.IndexOf('=') - 1); // 写入字段值
section.Field.Add(field); // 加入临时节点
}
else if (!string.IsNullOrEmpty(strTemp) && strTemp.IndexOf(';') != 0) // 确认临时数据是否为为无效字段
{
blSection = false; // 标记节点结束
break; // 退出本层循环
}
}
sections.Add(section); // 加入节点列表
}
else
{
strTemp = null; // 初始化临时数据
}
}
sr.Close(); // 关闭文件流
}
catch { }
}
/// <summary>
/// 保存配置信息
/// </summary>
private void SaveFile()
{
try
{
StreamWriter sw = new StreamWriter(strFilename, false, Encoding.Default); // 创建文件流用于写入配置文件
foreach (Section section in sections) // 循环进入各节点
{
sw.WriteLine(string.Format("[{0}]", section.Name)); // 写入节点信息
foreach (Field field in section.Field) // 循环进入各字段
{
sw.WriteLine(string.Format("{0}={1}", field.Name, field.Value)); // 写入字段信息
}
}
sw.Flush(); // 清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流。
sw.Close(); // 关闭文件流
}
catch { }
}
/// <summary>
/// 写入字符串值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="strValue">字符串值</param>
public void WriteString(string strSection, string strField, string strValue)
{
bool blSection = false, blField = false;
for (int y = 0; y < sections.Count; y++) // 遍历各节点
{
if (strSection.Equals(sections[y].Name)) // 找到并修改节点
{
blSection = true;
for (int x = 0; x < sections[y].Field.Count; x++) // 遍历各字段
{
if (strField.Equals(sections[y].Field[x].Name)) // 找到并修改字段
{
blField = true;
sections[y].Field[x].Value = strValue;
break;
}
}
if (!blField) // 未找到并添加字段
{
Field field = new Field();
field.Name = strField;
field.Value = strValue;
sections[y].Field.Add(field);
}
break;
}
}
if (!blSection) // 未找到并添加节点
{
Section section = new Section();
section.Name = strSection;
Field field = new Field();
field.Name = strField;
field.Value = strValue;
section.Field.Add(field);
sections.Add(section);
}
SaveFile(); // 保存配置信息
}
/// <summary>
/// 读取字符串值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="strDefault">默认值</param>
/// <returns>字符串值</returns>
public string ReadString(string strSection, string strField, string strDefault)
{
string result = null;
try
{
foreach (Section section in sections) // 遍历各节点
{
if (strSection.Equals(section.Name))
{
foreach (Field field in section.Field) // 遍历各字段
{
if (strField.Equals(field.Name))
{
result = field.Value;
break;
}
}
if (!string.IsNullOrEmpty(result)) { break; }
}
}
if (string.IsNullOrEmpty(result)) { result = strDefault; }
}
catch { }
return result;
}
/// <summary>
/// 写入整数值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="intValue">整数值</param>
public void WriteInt(string strSection, string strField, int intValue)
{
WriteString(strSection, strField, intValue.ToString());
}
/// <summary>
/// 读取整数值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="intDefault">默认值</param>
/// <returns>整数值</returns>
public int ReadInt(string strSection, string strField, int intDefault)
{
int result = 0;
try
{
result = int.Parse(ReadString(strSection, strField, intDefault.ToString()));
}
catch { }
return result;
}
/// <summary>
/// 写入浮点值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="fltValue">浮点值</param>
public void WriteFloat(string strSection, string strField, float fltValue)
{
WriteString(strSection, strField, fltValue.ToString());
}
/// <summary>
/// 读取浮点值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="fltDefault">默认值</param>
/// <returns>浮点值</returns>
public float ReadFloat(string strSection, string strField, float fltDefault)
{
float result = 0;
try
{
result = float.Parse(ReadString(strSection, strField, fltDefault.ToString()));
}
catch { }
return result;
}
/// <summary>
/// 写入布尔值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="blValue">布尔值</param>
public void WriteBool(string strSection, string strField, bool blValue)
{
WriteString(strSection, strField, blValue.ToString());
}
/// <summary>
/// 读取布尔值
/// </summary>
/// <param name="strSection">节名称</param>
/// <param name="strField">字段名称</param>
/// <param name="blDefault">默认值</param>
/// <returns>布尔值</returns>
public bool ReadBool(string strSection, string strField, bool blDefault)
{
bool result = false;
try
{
result = bool.Parse(ReadString(strSection, strField, blDefault.ToString()));
}
catch { }
return result;
}
}
}