C#中动态读写App.config配置文件 - zyloveyrf jinghuazhi.com
来源:互联网 发布:mmd美腿战队数据 编辑:程序博客网 时间:2024/06/18 12:36
在.Net中提供了配置文件,让我们可以很方面的处理配置信息,这个配置是XML格式的。而且.Net中已经提供了一些访问这个文件的功能。
1、读取配置信息
下面是一个配置文件的具体内容:
<!-- 此处显示用户应用程序和配置的属性设置。-->
<!-- 示例: -->
<add key="coal" value="一二三" />
<add key="inWellTime" value="5" />
.Net提供了可以直接访问(注意大小写)元素的方法,在这元素中有很多的子元素,这些子元素名称都是“add”,有两个属性分别是“key”和“value”。一般情况下我们可以将自己的配置信息写在这个区域中,通过下面的方式进行访问:
String ConString=System.Configuration.ConfigurationSettings.AppSettings["inWellTime"];
在AppSettings后面的是子元素的key属性的值,例如AppSettings["inWellTime"],我们就是访问这个子元素,它的返回值就是“5”,即value属性的值。
2、设置配置信息
如果配置信息是静态的,我们可以手工配置,要注意格式。如果配置信息是动态的,就需要我们写程序来实现。在.Net中没有写配置文件的功能,我们可以使用操作XML文件的方式来操作配置文件。
写了个WinForm中读写配置文件App.config的类
代码如下:
using System;
using System.Configuration;
using System.Xml;
using System.Da
namespace cn.zhm.common
{
///
/// ConfigClass 的摘要说明。
///
public class ConfigClass
{
public string strFileName;
public string configName;
public string configValue;
public ConfigClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string ReadConfig(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);
}
}
}
应用如下:
读取:
ConfigClass config = new ConfigClass();
string coal = config.ReadConfig("coal");
this.tbOpenFile.Text = config.ReadConfig("inWellTime");
写:
ConfigClass config = new ConfigClass();
//得到程序的config名:DataOperate.exe.config;
config.SetConfigName("DataOperate.exe.config");
config.SaveConfig("coal","三二一");
config.SaveConfig("inWellTime","10");
注意:当修改完App.config。文件后,程序中用到的App.config文件的“key”对应的“value”值需要重读,否则修改后修改并不能立即起作用,而要等下次程序重启后才可以读取到修改后的App.config属性值。
同时,winform读取app.config.可能会出现配置文件错误。修改如下:
<configuration>
<appSettings>
<add key="ConnectionString" value="server=127.0.0.1;uid=sa;pwd=xx;database=test"/>
</appSettings>
</configuration>
另外,修改的只需改app.config就可。编译时,会自动copy到相应exe目录下的config。
- C#中动态读写App.config配置文件 - zyloveyrf jinghuazhi.com
- C#中动态读写App.config配置文件
- c#实现RSA加密解密 - - 博客频道 - zyloveyrf jinghuazhi.com
- C#读写App.config配置文件
- C# 读写App.config配置文件的方法
- c# App.config配置文件读写操作
- c#,WinForm中读写配置文件App.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- C#读写config配置文件
- C#读写config配置文件
- C#读写config配置文件
- C#中读写App.Config文件
- wpf中有圆角的listbox
- 1.1马拦过河卒
- 备份与恢复Win2003的Active Directory(活动目录)数据库 - 京华志
- c#实现RSA加密解密 - - 博客频道 - zyloveyrf jinghuazhi.com
- WordPress主题制作全过程(六):制作footer.php
- C#中动态读写App.config配置文件 - zyloveyrf jinghuazhi.com
- WordPress 分类做导航栏,并高亮显示
- Dev-C++,Time to say goodbye ?(更新2012-02-04)
- JS倒计时组件
- Java中的ClassLoader
- ubuntu 步步为营之C编程初步
- 16. 3. 12. 鼠标拖拽及滑轮图形Move and scale graphical objects with a mouse on the panel
- 关于inline hook中函数地址计算的理解
- zencart调用wordpress文章到任何一个位置