更新本地 app.config 中的值

发布于 2024-12-02 12:58:07 字数 323 浏览 1 评论 0原文

我有一个 exe 可以从其本地 app.config 文件中读取一些值:

TargetDate = ConfigurationManager.AppSettings.Get("ThresholdDate");

// and try to update with the current date
ConfigurationManager.AppSettings.Set("ThresholdDate", "2011-09-01");

我认为它可以工作一次,但我根本没有看到 app.config 得到更新现在。

I've got an exe that reads some values from its local app.config file:

TargetDate = ConfigurationManager.AppSettings.Get("ThresholdDate");

// and try to update with the current date
ConfigurationManager.AppSettings.Set("ThresholdDate", "2011-09-01");

I thought it worked once, but am not seeing the app.config getting updated at all now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪少女 2024-12-09 12:58:07

我想你可以尝试这样的事情:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
//change the config value
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

我不确定更改配置值的语法,但我之前已经完成了 Add 并且我知道你可以进行删除,所以我想你可以做一个删除和添加的组合如下:

config.AppSettings.Settings.Remove("ThresholdDate");
config.AppSettings.Settings.Add("ThresholdDate", "2011-09-01");

I think you can try something like this:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
//change the config value
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

I am not sure abt the syntax for changing the config value but I have done an Add before and I know you can do a remove so I guess you could do a combination of remove and add like this:

config.AppSettings.Settings.Remove("ThresholdDate");
config.AppSettings.Settings.Add("ThresholdDate", "2011-09-01");
错々过的事 2024-12-09 12:58:07

看这里:如何答案是使用 C# 更改 App.config 文件运行时 - 在 Visual Studio IDE 中运行时,您将看不到 /bin/appname.exe.config 中保留的值。实际上,您必须进入 bin 目录并运行 exe。

所以这段代码实际上可以工作,只是不在调试模式下:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

Looking here: How to change App.config file run time using C# is the answer - while running in the visual studio IDE, you will not see the values persisted in the /bin/appname.exe.config. You actually have to go into the bin directory and run the exe.

So this code actually works, just not in debug mode:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
衣神在巴黎 2024-12-09 12:58:07
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

这仅在您不处于调试模式时才有效。尝试在 /debug/appName.exe 中运行 yung 代码,您将看到 appName.exe.config 中的更改,

欢呼!

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

this will work only while your not in debuging mode. try to run yung code in /debug/appName.exe and the you will se the changes in appName.exe.config

cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文