以前vs03项目中用到一个类:读写config文件配置信息
using System;
using System.Xml;
using System.IO;
namespace DataManager
{
/// <summary>
/// 修改和读取上传属性的方法
/// </summary>
public class Config
{
private string fileName = "";
private XmlDocument xmlDoc = new XmlDocument();
private XmlNode appnode = null;
public Config(string filePath)
{
fileName = filePath;
xmlDoc.Load(fileName);
XmlElement root = xmlDoc.DocumentElement;
for(int i = 0; i < root.ChildNodes.Count; i++)
{
if(root.ChildNodes[i].Name.ToLower() == "appsettings")
{
appnode = root.ChildNodes[i];
break;
}
}
}
写 void SetConfig(string key, string Value)
读string GetConfig(string key)
}
}
对应的config文件为:using System.Xml;
using System.IO;
namespace DataManager
{
/// <summary>
/// 修改和读取上传属性的方法
/// </summary>
public class Config
{
private string fileName = "";
private XmlDocument xmlDoc = new XmlDocument();
private XmlNode appnode = null;
public Config(string filePath)
{
fileName = filePath;
xmlDoc.Load(fileName);
XmlElement root = xmlDoc.DocumentElement;
for(int i = 0; i < root.ChildNodes.Count; i++)
{
if(root.ChildNodes[i].Name.ToLower() == "appsettings")
{
appnode = root.ChildNodes[i];
break;
}
}
}
写 void SetConfig(string key, string Value)
读string GetConfig(string key)
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="key1" value="1"/>
<add key="key2" value="0"/>
</appSettings>
</configuration>
应用示例如下:
<configuration>
<appSettings>
<add key="key1" value="1"/>
<add key="key2" value="0"/>
</appSettings>
</configuration>
Config cf = new Config(Application.ExecutablePath + ".config");
string key1Value = cf.GetConfig("key1");//get
key1Value += "key1 has been Edited";
cf.SetConfig("key1", key1Value);//set
string key1Value = cf.GetConfig("key1");//get
key1Value += "key1 has been Edited";
cf.SetConfig("key1", key1Value);//set
现在VS2005当然也可以用,不过05提供了更好的读写方法~