使用ConfigurationManager类读写配置文件
使用ConfigurationManager类 读写配置文件app.config,以下为代码:
- using System;
- using System.Configuration;
- static class Program
- {
- static void Main()
- {
- showConfig();
- UpdateAppSettings();
- showConfig();
- Console.ReadKey(true);
- }
- private static void showConfig()
- {
- string = ConfigurationManager.AppSettings["Directory"];
- Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "\n");
- }
- /// <summary>
- /// UpdateAppSettings
- /// </summary>
- public static void UpdateAppSettings()
- {
- // Get the configuration file.
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
- config.AppSettings.Settings["Directory"].Value = "tset";
- // Save the configuration file.
- config.AppSettings.SectionInformation.ForceSave = true;
- config.Save(ConfigurationSaveMode.Modified);
- // Force a reload of the changed section.
- ConfigurationManager.RefreshSection("appSettings");
- }
- using System;
- using System.Configuration;
- static class Program
- {
- static void Main()
- {
- showConfig();
- UpdateAppSettings();
- showConfig();
- Console.ReadKey(true);
- }
- private static void showConfig()
- {
- string = ConfigurationManager.AppSettings["Directory"];
- Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "\n");
- }
- /// <summary>
- /// UpdateAppSettings
- /// </summary>
- public static void UpdateAppSettings()
- {
- // Get the configuration file.
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
- config.AppSettings.Settings["Directory"].Value = "tset";
- // Save the configuration file.
- config.AppSettings.SectionInformation.ForceSave = true;
- config.Save(ConfigurationSaveMode.Modified);
- // Force a reload of the changed section.
- ConfigurationManager.RefreshSection("appSettings");
- }
app.config内容:
- view plaincopy to clipboardprint?
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="Directory" value="C:\Documents and Settings"/>
- </appSettings>
- </configuration>
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="Directory" value="C:\Documents and Settings"/>
- </appSettings>
- </configuration>
代码结果:app.config只能作为初始化的定义,工程生成后运行程序集名称.exe 修改生成后的 程序集名称.exe.Config文件
一开始调试时看到控制结果是想要的结果,但看app.config配置文件内容没变(vs2008 F5调试模式下是修改 程序集名称.vshost.exe.config配置文件)还以为是代码有问题,网上搜,也有人碰过到此现像,原来是自己没有理解到MSDN的说明。(还是有文化差异啊)如:
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
查看config.filePath值,即了解明白了。
4.0的类库: http://msdn.microsoft.com/en-us/library/ms134265(v=VS.100).aspx
注意:此类是.net2.0后新增。
必须要先在工程里添加system.configuration.dll程序集的引用。
(在解决方案管理器中右键点击工程名称,在右键菜单中选择添加引用,.net->组件名称->下即可找到)
添加引用后可以使用System.Configuration空间下的ConfigurationManager类.
引用资源:
Read/Write App.Config File with .NET 2.0
使用ConfigurationManager类读写配置文件的更多相关文章
- 使用java.util.Properties类读写配置文件
J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...
- System.ConfigurationManager类用于对配置文件的读取
http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...
- Java读写配置文件——Properties类的简要使用笔记
任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...
- C读写配置文件
在项目开发中,经常需要读取应用配置文件的初始化参数,用于应用在启动前进行一些初始化配置.比如:Eclipse,参数项包含主题.字体大小.颜色.Jdk安装位置.自动提示等.Eclispe配置的文件格式是 ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- 引用了System.Configuration命名空间,却找不到ConfigurationManager类
用ConfigurationManager类来读取应用程序配置文件的信息时,提示:System.Configuration命名空间下找不到ConfigurationManager类 查过资料后得知:要 ...
- python-ConfigParser模块【读写配置文件】
对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...
- ConfigParser 读写配置文件
一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...
- python利用ConfigParser读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section, section 下有option=value这样的键值 ...
随机推荐
- JAVA基础学习day27--反射机制
一.概述 1.1.概述 反射的概念: 在Java中的反射机制是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法; 这种动态获取信息以及动 ...
- CSS 包含选择器(九)
一.包含选择器 包含选择器中前后两部分之间以空格隔开,根据左侧选择符指定的祖先元素,然后在该元素下寻找匹配右侧的选择侧符的下级元素 定义包含选择器时,必须保证在HTML结构中第一个对象能够包含第二个对 ...
- java:String使用equals和==比较的区别
原文链接:http://www.cnblogs.com/tinyphp/p/3768214.html "=="操作符的作用 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的 ...
- Objective-C之集合对象的内存管理
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- javascript中的 类初始化,遍历for in 以及with的用法
<script type="text/javascript"> function member(name,gender){ this.name=name; this.g ...
- Jmeter调试工具---Debug Sampler
一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...
- 修复ORACLETNS-12545 因目标主机或对象不存在错误
现象: ORACLE启动不了,输入cmd->lsnrctl后,出现如下错误, 经查资料,发现是主机名可能解析有问题,后来在D:\oracle\ora92\network\admin下打开list ...
- 论近年来IT媒体的怪现象
之前在Svbtle上看过一篇文章干掉这帮搞IT新闻的!,作者因为CNET曲解原意,称Instagram要单方面售卖用户自己照片的乌龙事件,致使内心遭受严重刺激,怒吼出「科技媒体记者都应该被枪毙」的言论 ...
- Swift 2.0初探
转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...
- Effective Java 28 Use bounded wildcards to increase API flexibility
Get and Put Principle PECS stands for producer-extends(? extends T), consumer-super(? super T). For ...