好久没有写博了,我的园子都快荒废了。抽出点时间照看一下园子。
最近写程序正好须要保存程序设置的功能,以前也有好多程序需要此功能,不过当时我都是现用现写,今天索性写一个通用的保存程序设置的类。拿上来与大家分享。
说明:
这个类可以兼容.net cf ,保存的配置文件为XML格式。
此类为非静态类,使用时须要初始化,请看示例代码

/**//*保存设置*/
ProgramSetting ps = new ProgramSetting("c:\\setting.xml", "ApplicationName");

/**//*添加各种类型的健值对*/
ps.AddKey("SettionOne", "KeyOne", "KeyValue");
ps.AddKey("SettionTwo", "KeyTwo", true);
ps.AddKey("SettionOne\SettionChild", "KeyThree", 3.1415926);
ps.AddKey("SettionOne\SettionChild", "KeyThree", 12.24); //此处如果节的路径相同并且键的名称也相同,但值不同的,则覆盖原值。
ps.AddKey<object>("SettionTwo\GroupTwo", "KeyOne", object);
ps.Save();//将内存流中的设置保存到磁盘



/**//*读取设置*/
ProgramSetting ps = new ProgramSetting("c:\\setting.xml", "ApplicationName");
int i = ps.ReadKeyForInt("SettionName","KeyName",DefaultValue);
bool b = ps.ReadKeyForBoolean("SettionName","KeyName",DefaultValue);
double d = ps.ReadKeyForDouble("SettionName","KeyName",DefaultValue);
代码下载:
单击下载