app.config项目配置文件读写
来源:互联网 发布:网络监控摄像头价格 编辑:程序博客网 时间:2024/04/28 14:01
来自:http://blog.csdn.net/fengyaxuan3550/article/details/7662893
使用.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");//重新加载新的配置文件
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;using System.Text.RegularExpressions;namespace DaemonApp{ class Utilities { public const string PATH_PREFIX = "Path_"; public Utilities() { ClearPathCustomAppSettings(); } public void SetPathCustiomAppSettings(string key, string value) { AddCustomAppSettings(PATH_PREFIX + key, value); } public void AddCustomAppSettings(string key, string value) { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); if (ConfigurationManager.AppSettings[key] == null) { config.AppSettings.Settings.Add(key, value); } else { config.AppSettings.Settings[key].Value = value; } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件 } public string GetCustomAppSettings(string key) { return ConfigurationManager.AppSettings[key]; } private void ClearPathCustomAppSettings() { string key=""; System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); KeyValueConfigurationCollection collections =config.AppSettings.Settings; foreach (KeyValueConfigurationElement item in collections) { string pattern = string.Format("^{0}.+",PATH_PREFIX); if (!Regex.IsMatch(item.Key, pattern)) continue; config.AppSettings.Settings.Remove(key); } } }}
- app.config项目配置文件读写
- app.config项目配置文件读写
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写配置文件app.config
- 读写应用程序配置文件app.config
- C#读写App.config配置文件
- c#,WinForm中读写配置文件App.config
- C#中动态读写App.config配置文件
- App.config和配置文件的读写
- C# 读写App.config配置文件的方法
- c# App.config配置文件读写操作
- [C#] 读写App.config配置文件的方法
- [C#] 读写App.config配置文件的方法
- android游戏中实现渐隐和渐现动画
- orcale自增长
- python 进程间通信
- 提高网站打开速度的7大秘籍
- 在Windows Azure中创建网站 - ASP和FTP
- app.config项目配置文件读写
- 转]写一个完美的strcpy / strlen 函数
- simpleDateFormat格式
- Ext2Fsd Project
- tar 知识点
- hdu 1337 The Drunk Jailer(规律)
- android优化
- 【Java常用类库】_Arrays笔记
- 导出报表时如何屏蔽单元格中的超链接