Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中退出应用程序后未保存appSettings#_C# - Fatal编程技术网

C# 在c中退出应用程序后未保存appSettings#

C# 在c中退出应用程序后未保存appSettings#,c#,C#,我正在尝试在运行时使用ConfigurationManager保存应用程序设置。我使用以下代码更新配置中的键值对: private bool SetAppConfigSetting(string Key, string Value) { bool result = false; try { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguratio

我正在尝试在运行时使用ConfigurationManager保存应用程序设置。我使用以下代码更新配置中的键值对:

private bool SetAppConfigSetting(string Key, string Value)
{
    bool result = false;
    try
    {
        System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        config.AppSettings.Settings.Remove(Key);
        var kvElem = new KeyValueConfigurationElement(Key, Value);
        config.AppSettings.Settings.Add(kvElem);

        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified);

        // Force a reload of a changed section.
        ConfigurationManager.RefreshSection("appSettings");

        result = true;
    }
    finally
    { }
    return result;
}
每次我调用它时,它都会删除现有的键值对,并用新的应用程序设置替换它

但是,我发现只有在我的代码运行时,
appSettings
才会在
Application.vshost.exe.config
中得到更新,但是只要代码点击
Application.Exit()
,那么
appSettings
就会全部消失


我哪里出了问题?

尝试了此修改,但仍然无效。我发现了问题。这是因为我是在调试模式而不是发布模式下构建代码的。当我将构建模式更改为Release时,我可以看到AppSettings被保存到Application.exe.Config文件中。谢谢!