使用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

在程序里修改配置文件

C#读写app.config中的数据

使用ConfigurationManager类读写配置文件的更多相关文章

  1. 使用java.util.Properties类读写配置文件

    J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...

  2. System.ConfigurationManager类用于对配置文件的读取

    http://blog.csdn.net/ligenyingsr/article/details/54095986 System.ConfigurationManager类用于对配置文件的读取.其具有 ...

  3. Java读写配置文件——Properties类的简要使用笔记

    任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...

  4. C读写配置文件

    在项目开发中,经常需要读取应用配置文件的初始化参数,用于应用在启动前进行一些初始化配置.比如:Eclipse,参数项包含主题.字体大小.颜色.Jdk安装位置.自动提示等.Eclispe配置的文件格式是 ...

  5. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  6. 引用了System.Configuration命名空间,却找不到ConfigurationManager类

    用ConfigurationManager类来读取应用程序配置文件的信息时,提示:System.Configuration命名空间下找不到ConfigurationManager类 查过资料后得知:要 ...

  7. python-ConfigParser模块【读写配置文件】

    对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...

  8. ConfigParser 读写配置文件

    一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...

  9. python利用ConfigParser读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值 ...

随机推荐

  1. 高级iOS面试题

    非标准答案 2 1: 类方法是可以直接通过类名直接调用,无需进行实例化对象.类方法是以+开头2. 实例方法,需要显示实例化对象,为对象分配堆栈空间,并通过对象实例调用实例方法3. RUNTIME 是在 ...

  2. CSS 伪类和伪对象选(五)

    一.伪选择器 伪选择器包括:伪类选择器和伪对象选择器,以冒号(:)作为前缀,冒号后紧跟伪类或者伪对象名称,冒号前后没有空格,否则解析为包含选择器 如: div:hover{ font-size:12p ...

  3. 【转】IOS开发中图片资源使用png还是jpg格式

    对于iOS本地应用程序来说最简单的答案就是始终使用PNG,除非你有非常非常好的理由不用它. 当iOS应用构建的时候,Xcode会通过一种方式优化.png文件而不会优化其它文件格式.它优化得相当的好 他 ...

  4. 全球最低功耗蓝牙单芯片DA14580的软件体系 -RW内核和消息处理机制

    上一篇文章<蓝牙单芯片DA14580的硬件架构和低功耗>阐述了DA14580的硬件架构和低功耗的工作原理.本文文章阐述该平台的软件体系,并着重分析消息事件的处理机制. 一.DA14580S ...

  5. 提示“正尝试安装的adobe flash player不是最新版本”的解决方法

    尼玛,今天下午遇到一个比较奇怪的现象,我电脑的flash的不能正常使用了,我下载了一个重新安装了下,但还是不行.然后我又卸载了,重新安装flash,重启电脑,还是不行...而且在安装flash pla ...

  6. 【转载】主数据管理(MDM)与元数据管理

    主数据(Master Data)和元数据(Meta Data)是两个完全不同的概念.元数据是指表示数据的相关信息,比如数据定义等,而主数据是指实例数据,比如产品目录信息等.比如,某省地税开发了一套 征 ...

  7. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  8. JavaScript Patterns 2.9 Coding Conventions

    It’s important to establish and follow coding conventions—they make your code consistent, predictabl ...

  9. python数据结构-基本数据类型

  10. Tomcat报java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler

    最近在生产环境部署Tomcat的时候,在启动的时候,在控制台报"java.lang.ClassNotFoundException: 1catalina.org.apache.juli.Fil ...