使用ConfigurationManager类 读写配置文件app.config (转)
使用ConfigurationManager类 读写配置文件app.config,以下为代码:
view plaincopy to clipboardprint?
01.using System;
02.using System.Configuration;
03.
04.static class Program
05. {
06. static void Main()
07. {
08. showConfig();
09. 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. }
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?
01.<?xml version="1.0" encoding="utf-8" ?>
02.<configuration>
03. <appSettings>
04. <add key="Directory" value="C:\Documents and Settings"/>
05. </appSettings>
06.</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类.
引用资源:
http://www.ajaxline.com/node/258
http://www.codeproject.com/KB/cs/SystemConfiguration.aspx
http://www.cnblogs.com/interboy/archive/2007/04/22/722894.html 使用ConfigurationManager类读写web.config
http://yueqing.blog.163.com/blog/static/32082592009813111946229/ 多个示例代码实现
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhou__zhou/archive/2007/08/14/1742091.aspx
今天测试的时候发现动态设置配置文件的时候会出现读写不一致的情况,以前测试的时候只是简单的测试,比如地点和密码很少改,有的时候只改其中的一个,所以没有出现问题。
今天出现的问题就是自己明明动态的写进了配置文件中一些数据,经过我的查看可以证明我已经写进A.exe.config文件了,可是就是从中读不出想要的数据,数据总是慢一拍,是我上次设置的值。经过我对上面的红色字体的解释理解,才豁然开朗。
用自己的理解解释今天遇到的问题:我动态的写进配置文件内容了,代码在上面的博客中提及,我写进的文件时A.exe.config,不过读的时候确实从A.vshost.exe.config 中读出来的,但是我一关闭VS时A.exe.config的内容就会更新至 A.vshost.exe.config中,下次再执行的时候还是从A.vshost.exe.config中读,所以显示总是慢一些,感觉怪怪的。现在终于明白了,可能是打成程序包的时候就不会出现这些问题了。需要我的实践去证明,等待后续。

浙公网安备 33010602011771号