gpt4 book ai didi

c# - App.Config 更改值

转载 作者:IT王子 更新时间:2023-10-29 03:56:38 25 4
gpt4 key购买 nike

这是我的 App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="lang" value="English"/>
</appSettings>
</configuration>

我用这段代码做了改变

lang = "Russian";
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang);
}

但它没有改变。我做错了什么?

最佳答案

AppSettings.Set 不会保留对配置文件的更改。它只是在内存中更改它。如果您在 System.Configuration.ConfigurationManager.AppSettings.Set("lang", lang); 上放置断点,并为 System.Configuration.ConfigurationManager.AppSettings[0]< 添加监视 当该行代码运行时,您会看到它从“英语”变为“俄语”。

以下代码(在控制台应用程序中使用)将保留更改。

class Program
{
static void Main(string[] args)
{
UpdateSetting("lang", "Russian");
}

private static void UpdateSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save();

ConfigurationManager.RefreshSection("appSettings");
}
}

来自这篇文章: http://vbcity.com/forums/t/152772.aspx

One major point to note with the above is that if you are running this from the debugger (within Visual Studio) then the app.config file will be overwritten each time you build. The best way to test this is to build your application and then navigate to the output directory and launch your executable from there. Within the output directory you will also find a file named YourApplicationName.exe.config which is your configuration file. Open this in Notepad to see that the changes have in fact been saved.

关于c# - App.Config 更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11149556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com