1

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.

14
  • What does the InvalidOperationException say? Commented May 13, 2015 at 15:35
  • the Message was, like I mentioned: Method failed with unexpected error code 1. and there was no InnerException. Not much help indeed. Commented May 13, 2015 at 15:50
  • try adding settings.Remove(key); before adding Commented May 13, 2015 at 16:07
  • @Sachu Thanks for your suggestion. However, I just tried, and the same exception occurs. Commented May 13, 2015 at 16:12
  • is the config file on a network drive or your local machine? Commented May 13, 2015 at 16:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

if you could add your remark about the shared disk to your solution, I would be glad to accept it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.