码迷,mamicode.com
首页 > 其他好文 > 详细

C#读取和写入配置文件

时间:2014-06-26 12:37:35      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   文件   set   cti   

使用.Net2.0中的ConfigurationManager可以方便的实现对配置app.config的读取和写入。

ConfigurationManager默认没有自动载入项目,使用前必须手动添加,方法如下:

项目->引用->添加引用->选择System.configuration

 

1.使用ConfigurationManager读配置文件

我们可以将简单的配置内容放到app.config中的appSettings节点中如下:

<appSettings>
  <add key="GPRS.Port1" value="5002"/>
  <add key="GPRS.Port2" value="5003"/>
  <add key="GPRS.Port3" value="5004"/>
</appSettings>

然后在程序中使用ConfigurationManager.AppSettings["GPRS.Port1"]来读取GPRS.Port1的值

 

2.使用ConfigurationManager写配置文件

如何想要把修改过的GPRS.Port1的值存放回app.config,可以使用下面的代码

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["GPRS.Port1"].Value = “xxxxx”;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件

 

 

如需转载,请注明本文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor

C#读取和写入配置文件,布布扣,bubuko.com

C#读取和写入配置文件

标签:blog   http   使用   文件   set   cti   

原文地址:http://www.cnblogs.com/hyshareex/p/3809352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!