使用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这样的键值 ...
随机推荐
- CocoaPod出现-bash: pod: command not found 解决办法
从过年来到公司 就不用自己电脑了 之前一直自己带电脑 昨天随便建了一个demo 使用cocoapods 发现 -bash: pod: command not found 刚开 ...
- 【读书笔记】iOS-Objective-C对C的扩展基础知识
一,Xcode的.m扩展名表示文件含有Objective-C代码,应由Objective-C编译器处理.C编译器处理名称以.c结尾的文件,而C++编译器处理.cpp文件.在Xcode中,所有这些编译工 ...
- Service的生命周期&Service绑定方法
服务的生命周期: 一.采用start的方式开启服务 生命周期如下: 开启服务: onCreate()--> onStartCommand() (onStart()过时了)---> onDe ...
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
前言:使用NSURLSessionDownloadTask满足不这个需要离线断点续传的下载需求,所以这里就需要使用NSURLSessionDataTask的代理方法来处理下载大文件,并且实现离线断点续 ...
- Android开发艺术探索学习笔记(十一)
第十一章 Android的线程和线程池 从用途上来说,线程分为子线程和主线程,主线程主要处理和界面相关的事情,而子线程往往用于执行耗时的操作.AsyncTask,IntentService,Hand ...
- java微信接口之二—获取用户组
一.微信获取用户组接口简介 1.请求 该请求也是GET方式请求.请求的url格式如下: https://api.weixin.qq.com/cgi-bin/groups/get?access_toke ...
- 标准C IO函数和 内核IO函数 效率(时间)比较
前言 标准C提供的文件相关的IO函数,除标准错误输出是不带缓冲的(可以尽快的将错误消息显示出来)之外,所有与终端相关的都是行缓冲,其余都是全缓冲的. 我们可以使用setbuf,setvbuf改变指定流 ...
- Android 开发框架
Android 开发框架包括基本的应用功能开发.数据存储.网络访问三大块. 1 应用方面 一般而言,一个标准的Android 程序包括Activity.Broadcast Intent Receive ...
- 如何在TFS的过程模板中添加报表
在新建团队项目的过程中,TFS的"新建团队项目向导"会根据用户选择的过程模板类型(CMMI, Scrum,Agile等)自动为团队项目创建一个SSRS(SQL Server Rep ...
- [转]Display PDF within web browser using MVC3
本文转自:http://www.codeproject.com/Tips/697733/Display-PDF-within-web-browser-using-MVC Introduction I ...