How to save changes in App.config?

2019-08-11 05:28发布

问题:

I am trying to save settings in the App.config file, but am getting InvalidOperationException. I am following the example from MSDN.

This is my code:

private void button_Update_Click(object sender, EventArgs e)
{
    string Key = textBox_Key.Text.Trim();
    string Value = textBox_Value.Text.Trim();//bug: should use control textBox_NewValue
    if (Key != "" && Value != "")
    {
        try
        {
            // write new value to app.config:
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            KeyValueConfigurationCollection settings = config.AppSettings.Settings;
            if (settings[Key] == null)
                settings.Add(Key, Value);
            else
                settings[Key].Value = Value;
            //config.Save(ConfigurationSaveMode.Modified); //-->Exception: Method failed with unexpected error code 1.
            config.Save(); //-->Exception: Method failed with unexpected error code 1.
            ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
            // feedback:
            textBox_Value.Text = ConfigurationManager.AppSettings[Key];
            textBox_NewValue.Text = "";
        }
        catch (ConfigurationErrorsException exc)
        {
        }//debug breakpoint
        catch (Exception exc)//--> InvalidOperationException
        {
        }//debug breakpoint
    }
    return;
}

Might there be something that it is not possible to update a file like App.config while it is in use and locked by the running application?


As commented below, my question seems a duplicate of ConfigurationManager.save() fails. On a local disk and / or running the .exe directly (not in VS Debug mode) the exception does not occur.

Unfortunately, I do also not see an updated value in App.config, but that seems an unrelated question.


After fixing my own bug (see comment above) I can confirm that the new value is indeed written, to the file {app}.exe.config, on a non-shared disk.

回答1:

For Winforms projects, when you compile your project the app.config file is copied to the output directory as <appname>.exe.config. Any runtime changes made to the configuration are made to this file (and not the app.config in your source code directory). This is the file that should be distributed with your application; if you're using ClickOnce this file will go with your application to become the config file used by the deployment.

Also worth noting for debugging purposes (which is really your use case here for the moment), as long as you're running inside Visual Studio, the actual file modified will be <appname>.vshost.exe.config. You can verify this by running inside VS and checking the .vshost.exe.config file, and then running the .exe from the directory directly and monitoring the .exe.config file.

As for the InvalidOperationException, there is a known problem with trying to access the app.config file in a VirtualBox shared folder, although the only solution seems to be to use a local directory.