使用ConfigurationManager类 读写配置文件app.config,以下为代码:

  1. using System;
  2. using System.Configuration;
  3.  
  4. static class Program
  5. {
  6. static void Main()
  7. {
  8. showConfig();
  9. UpdateAppSettings();
  10. showConfig();
  11.  
  12. Console.ReadKey(true);
  13. }
  14.  
  15. private static void showConfig()
  16. {
  17. string = ConfigurationManager.AppSettings["Directory"];
  18. Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "\n");
  19. }
  20.  
  21. /// <summary>
  22. /// UpdateAppSettings
  23. /// </summary>
  24. public static void UpdateAppSettings()
  25. {
  26. // Get the configuration file.
  27. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  28. Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
  29. config.AppSettings.Settings["Directory"].Value = "tset";
  30.  
  31. // Save the configuration file.
  32. config.AppSettings.SectionInformation.ForceSave = true;
  33. config.Save(ConfigurationSaveMode.Modified);
  34. // Force a reload of the changed section.
  35. ConfigurationManager.RefreshSection("appSettings");
  36. }
  37.  
  38.   using System;
  39.   using System.Configuration;
  40.  
  41.   static class Program
  42. {
  43. static void Main()
  44. {
  45. showConfig();
  46. UpdateAppSettings();
  47. showConfig();
  48.  
  49. Console.ReadKey(true);
  50. }
  51.  
  52. private static void showConfig()
  53. {
  54. string = ConfigurationManager.AppSettings["Directory"];
  55. Console.WriteLine("AppSetting配置节 Path key的value为:" + dir + "\n");
  56. }
  57.  
  58. /// <summary>
  59. /// UpdateAppSettings
  60. /// </summary>
  61. public static void UpdateAppSettings()
  62. {
  63. // Get the configuration file.
  64. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  65. Console.WriteLine("config.FIlePath: " + config.FilePath + "\n");
  66. config.AppSettings.Settings["Directory"].Value = "tset";
  67.  
  68. // Save the configuration file.
  69. config.AppSettings.SectionInformation.ForceSave = true;
  70. config.Save(ConfigurationSaveMode.Modified);
  71. // Force a reload of the changed section.
  72. ConfigurationManager.RefreshSection("appSettings");
  73. }

app.config内容:

  1. view plaincopy to clipboardprint?
  2. <?xml version="1.0" encoding="utf-8" ?>
  3. <configuration>
  4. <appSettings>
  5. <add key="Directory" value="C:\Documents and Settings"/>
  6. </appSettings>
  7. </configuration>
  8. <?xml version="1.0" encoding="utf-8" ?>
  9. <configuration>
  10. <appSettings>
  11. <add key="Directory" value="C:\Documents and Settings"/>
  12. </appSettings>
  13. </configuration>

代码结果:app.config只能作为初始化的定义,工程生成后运行程序集名称.exe 修改生成后的 程序集名称.exe.Config文件

一开始调试时看到控制结果是想要的结果,但看app.config配置文件内容没变(vs2008 F5调试模式下是修改 程序集名称.vshost.exe.config配置文件)还以为是代码有问题,网上搜,也有人碰过到此现像,原来是自己没有理解到MSDN的说明。(还是有文化差异啊)如:

  1. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  2. 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. JAVA基础学习day27--反射机制

    一.概述 1.1.概述 反射的概念: 在Java中的反射机制是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法; 这种动态获取信息以及动 ...

  2. CSS 包含选择器(九)

    一.包含选择器 包含选择器中前后两部分之间以空格隔开,根据左侧选择符指定的祖先元素,然后在该元素下寻找匹配右侧的选择侧符的下级元素 定义包含选择器时,必须保证在HTML结构中第一个对象能够包含第二个对 ...

  3. java:String使用equals和==比较的区别

    原文链接:http://www.cnblogs.com/tinyphp/p/3768214.html "=="操作符的作用 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的 ...

  4. Objective-C之集合对象的内存管理

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  5. javascript中的 类初始化,遍历for in 以及with的用法

    <script type="text/javascript"> function member(name,gender){ this.name=name; this.g ...

  6. Jmeter调试工具---Debug Sampler

    一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...

  7. 修复ORACLETNS-12545 因目标主机或对象不存在错误

    现象: ORACLE启动不了,输入cmd->lsnrctl后,出现如下错误, 经查资料,发现是主机名可能解析有问题,后来在D:\oracle\ora92\network\admin下打开list ...

  8. 论近年来IT媒体的怪现象

    之前在Svbtle上看过一篇文章干掉这帮搞IT新闻的!,作者因为CNET曲解原意,称Instagram要单方面售卖用户自己照片的乌龙事件,致使内心遭受严重刺激,怒吼出「科技媒体记者都应该被枪毙」的言论 ...

  9. Swift 2.0初探

    转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心. 今年6月,一年一度的WWDC大会如期而至,在大会上Apple发布了Swift 2.0,引 ...

  10. 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 ...