配置文件分为两种 :一种是winform应用程序的配置文件, 一种是web的配置文件.

  两种配置文件最大的区别是web的配置文件更新之后会时时更新, 应用程序的配置文件不会实时更新.

  更新应用程序的配置文件之后需刷新

  ConfigurationManager.RefreshSection("appSettings");// 刷新命名节,在下次检索它时将从磁盘重新读取它。

操作方法:

旧方法:

配置文件:

<configuration>
<appSettings>
<add key="name" value="sqlserver"/>
</appSettings>
</configuration>

后台程序读取值:

string s=System.Configuration.ConfigurationSettings.AppSettings["name"];

修改配置文件的值:

/// <summary>
/// 更新配置文件信息
/// </summary>
/// <param name="name">配置文件字段名称</param>
/// <param name="Xvalue">值</param>
private void UpdateConfig(string name,string Xvalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath + ".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='"+name+"']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value", Xvalue);
doc.Save(Application.ExecutablePath + ".config");
}

向配置文件插入值:

///<summary>
///向.config文件的appKey结写入信息AppValue 保存设置
///</summary>
///<param name="AppKey">节点名</param>
///<param name="AppValue">值</param>
Private void SetValue(String AppKey,String AppValue)
{
Xmldocument xDoc=new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath+”.config”);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode=xDoc.SelectSingleNode(“//appSettings”);
xElem1=(XmlElement)xNode.SelectSingleNode(“//add[@key=’”+AppKey+”’]”);
if(xElem1!=null)
xElem1.SetAttribute(“value”,AppValue);
else
{
xElem2=xdoc.CreateElement(“add”);
xElem2.SetAttribute(“key”,AppKey);
xElem2.setAttribute(“value”,AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath+”.config”);
}

上述方法在FrameWork2.0已经明确表示此属性已经过时。并建议改为ConfigurationManager或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.

新方法:

要想调用ConfigurationManager必须要先在工程里添加system.configuration.dll程序集的引用。(在解决方案管 理器中右键点击工程名称,在右键菜单中选择添加引用,.net TablePage下即可找到)添加引用后可以用 String str = ConfigurationManager.AppSettings["Key"]来获取对应的值了。

后台读取值;

String str = ConfigurationManager.AppSettings["Key"];

更新配置文件:

Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

  //添加

  cfa.AppSettings.Settings.Add("key", "Name")

  //修改

  cfa.AppSettings.Settings["BrowseDir"].Value = "name";
//保存
cfa.Save();
//刷新,刷新命名节,在下次检索它时将从磁盘重新读取它。
ConfigurationManager.RefreshSection("appSettings");//

winform应用程序要想不退出程序查看修改值,必须记住要调用刷新节点!!!

.net 对配置文件内容的操作的更多相关文章

  1. java文件操作(普通文件以及配置文件的读写操作)

    转自:java文件操作(普通文件以及配置文件的读写操作) 读取普通文件 : /** * xiangqiao123欢迎你 如果对代码有疑问可以加qq群咨询:151648295 * * 读取MyFile文 ...

  2. 修改jar包配置文件的正确操作,jar包解压出来的文件夹重新打成jar,不依靠开发工具!!!!

    修改jar包配置文件的正确操作,有的时候通过一些解压工具可以对内部的文件进行修改,但是有时候会无效.这就很烦了 一.背景:       有一个springboot项目,事先我已经用编译好打成jar包以 ...

  3. 【Gin-API系列】配置文件和数据库操作(三)

    我们前面已经实现了API的基础版本,能对参数校验和返回指定数据,这一章,我们将对主机和交换机进行建模,存入数据库. 考虑到数据库安装和使用的简便性,我们采用文档存储结构的MongoDB数据库. Mon ...

  4. java读取配置文件内容

    利用com.typesafe.config包实现 <dependency> <groupId>com.typesafe</groupId> <artifact ...

  5. 011PHP文件处理——文件处理 文件内容分页操作类

    <?php /** * 文件内容分页操作类: */ //访问地址:http://basicphp.com/006file/011.php?&page=1 class StrPage { ...

  6. CSharp读取json配置文件内容

    步骤 读取配置文件转换成字符串,代码如下 string contents = System.IO.File.ReadAllText("config.json"); 注意:该语句会抛 ...

  7. Python+selenium之读取配置文件内容

    Python+selenium之读取配置文件内容 Python支持很多配置文件的读写,此例子中介绍一种配置文件的读取数据,叫ini文件,python中有一个类ConfigParser支持读ini文件. ...

  8. tornado 06 数据库—ORM—SQLAlchemy——基本内容及操作

    tornado 06 数据库—ORM—SQLAlchemy——基本内容及操作 一. ORM #在服务器后台,数据是要储存在数据库的,但是如果项目在开发和部署的时候,是使用的不同的数据库,该怎么办?是不 ...

  9. 得到properties配置文件内容

    代码: 1.配置文件内容 2.文件所在项目中位置: 3.java代码: 01.得到键值对: @Test public void getProp() { Properties prop = new Pr ...

随机推荐

  1. [Ruby on Rails Issue] When Setting Sqlite version on the Gemfile, Show error "An error occurred while installing sqlite3 ",

    Issue: Gem files will remain installed in /tmp/bundler20140825-31835-p0c0p/sqlite3-1.3.9/gems/sqlite ...

  2. JAVA基础英语单词表(中)

    factory                                 / 'fæktəri /                    工厂 fetch                     ...

  3. Android——C语言、JNI与低层调用

    JNI java native interface c的基本数据类型 int:32位,能表示的数字是2的32次方个 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是2的31次方 ...

  4. 【JAVA - SSM】之MyBatis的ParameterType的使用

    在MyBatis的Mapper.xml文件中,参数的表示方法有两种:一种是使用 "#{XXX}" 的方式表示的,另一种是使用 "${XXX}" 的方式表示的.今 ...

  5. Ueditor自定义默认宽度高度

    如题. 最近需要使用到网页后台富文本编辑器.经过同学推荐,最后决定使用百度家的Ueditor. 官方地址:http://ueditor.baidu.com/website/ 贴吧地址:ueditor讨 ...

  6. Cocos2d 3.0继承自Sprite的类在addChild后出现故障

    当继承自Sprite的类被addChild到其它的Node里后出现例如以下图问题,说明没有调用父类Sprite::init()的方法.由于父类Sprite里的_textureAtlas须要初始化为nu ...

  7. android-配置虚拟机Virtual device

    Android的应用程序是基于virtual device运行的,在运行一个android的应用程序之前先要配置要virtual device

  8. hdu4485 B-Casting(mod运算)

    B-Casting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. plupload使用指南(转)

    转自http://www.cnblogs.com/2050/p/3913184.html 现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SWFUpload显然就有点过时了,毕 ...

  10. Papers

    Research on Semantic Text Mining Based on Domain Ontologyhttp://link.springer.com/chapter/10.1007/97 ...