动态地更改app.config文件中值中值、文件、动态、config

由网友(心上锁)分享简介:我想修改的值在app.config中appSetting部分。所以我写了,I want to modify a value in appSetting section in app.config. So i wrote,Console.WriteLine(ConfigurationManager.AppSettin...

我想修改的值在app.config中appSetting部分。所以我写了,

I want to modify a value in appSetting section in app.config. So i wrote,

Console.WriteLine(ConfigurationManager.AppSettings["name"]);
Console.Read();
Configuration config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
config.AppSettings.Settings["name"].Value = "raja";       
config.Save(ConfigurationSaveMode.Modified);  
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
Console.Read();

以上code执行后,我验证了的app.configname元素的值是否已更改或不。但没有任何变化。

after the execution of above code, i verified the app.config whether the value of "name" element has been changed or not. but no change.

什么是错我的code?或者是有任何其他方式做到这一点?

what is the wrong with my code? or is there any other way to do this?

推荐答案

这code对我的作品:

This code works for me:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    config.AppSettings.Settings["test"].Value = "blah";       
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");

注:如果您使用F5运行它不会更新解决方案项目的app.config,但.exe.config之一的bin /文件夹

Note: it doesn't update the solution item 'app.config', but the '.exe.config' one in the bin/ folder if you run it with F5.

阅读全文

相关推荐

最新文章