使用ConfigurationManager类 读写配置文件
2007-08-14 10:09
344 查看
使用ConfigurationManager类 读写配置文件app.config,以下为代码:
app.config内容:
代码结果: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
/article/4980162.html 使用ConfigurationManager类读写web.config
http://yueqing.blog.163.com/blog/static/32082592009813111946229/ 多个示例代码实现
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内容:
<?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
/article/4980162.html 使用ConfigurationManager类读写web.config
http://yueqing.blog.163.com/blog/static/32082592009813111946229/ 多个示例代码实现
相关文章推荐
- 使用XML读写删除功能来实现资源文件配置
- 使用java.util.Properties类读写配置文件
- 使用反射和范型来动态读写应用程序配置文件
- QT 使用QSettings读写ini配置文件
- 使用IniEditor读写INI类型配置文件
- 使用IniEditor读写INI类型配置文件
- 使用ConfigurationManager类 读写配置文件
- 使用泛型对读写配置文件的方法进行封装
- 使用XML读写删除功能来实现资源文件配置
- perl亲身试验ini---使用perl读写配置文件
- QT 使用QSettings读写ini配置文件
- 使用ExeConfigurationFileMap读写配置文件
- 使用IniEditor读写INI类型配置文件
- ASP.NET 使用ConfigurationManager读写配置文件
- .Net2.0 使用ConfigurationManager读写配置文件
- 使用ConfigurationManager类 读写配置文件
- .NET 2.0中,使用ConfigureManager读写配置文件
- 使用WindowsAPI读写配置文件
- 使用ConfigurationManager类读写配置文件
- QT 使用QSettings 读写ini配置文件