动态地更改app.config文件中值 [英] Change the value in app.config file dynamically

查看:350
本文介绍了动态地更改app.config文件中值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改的值在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.

这篇关于动态地更改app.config文件中值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆