public class ConfigOperator
{
public string strFileName;
public string configName;
public string configValue;
public ConfigOperator()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string ReadConfig1(string configKey)
{
configValue = "";
configValue = ConfigurationSettings.AppSettings[""+configKey+""];
return configValue;
}
//得到程序的config文件的名称以及其所在的全路径
public void SetConfigName(string strConfigName)
{
configName = strConfigName;
//获得配置文件的全路径
GetFullPath();
}
public void GetFullPath()
{
//获得配置文件的全路径
strFileName=AppDomain.CurrentDomain.BaseDirectory.ToString()+configName;
}
public void SaveConfig(string configKey,string configValue)
{
XmlDocument doc=new XmlDocument();
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes=doc.GetElementsByTagName("add");
for(int i=0;i<nodes.Count;i++)
{
//获得将当前元素的key属性
XmlAttribute att=nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value== ""+configKey+"")
{
//对目标元素中的第二个属性赋值
att=nodes[i].Attributes["value"];
att.Value=configValue;
break;
}
}
//保存上面的修改
doc.Save(strFileName);
}
public string ReadConfig(string configKey)
{
string tempStr = "";
XmlDocument doc = new XmlDocument();
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "" + configKey + "")
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
tempStr= att.Value;
}
}
//保存上面的修改
return tempStr;
}
}
分享到:
相关推荐
C#读写App.config的工具类
使用app.config进行配置必须材料
c#读写app.config中的数据
一个小demo,对于需要读写app.config的朋友绝对非常有用。里面有各种高级读写方式。
C# 读写加密狗 实例源码(硬件读写)
C# CAD 读写扩展记录源码
C# XML & app.config 操作类
主要介绍了详解C#如何读写config配置文件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
C# 数据采集器读写 实例源码(硬件读写)
VS中C#读取app.config数据库配置字符串的三种方法
C#如何操作app.config中的数据C#如何操作app.config中的数据
你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了App.config配置文件。 本文探讨用代码的方式访问 App.config 的方法。关于 App.config 的使用远比上面提到的用途...
C# IC卡读写 实例源码(硬件读写)
C#读写xml的源码
c#读写注册表 文档和源码 内有详细说明和代码
源码说明: C#编写的和西门子OPCUA服务器进行通信的程序源码 包含读写数据,多个读写,多个注册node,多个订阅等功能 包含完整代码和注释 非常适合借鉴学习 适合人群:新手及有一定经验的开发人员
C# 编写读写USB 源码 C# 编写读写USB 源码
config 文件 读写 介绍 文档中有介绍 可以借鉴下
C# WinForm 读写XML文件 C# WinForm 读写XML文件