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.

Community
  • 1
  • 1
Roland
  • 4,619
  • 7
  • 49
  • 81
  • What does the `InvalidOperationException` say? – Ron Beyer May 13 '15 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. – Roland May 13 '15 at 15:50
  • try adding settings.Remove(key); before adding – Sachu May 13 '15 at 16:07
  • @Sachu Thanks for your suggestion. However, I just tried, and the same exception occurs. – Roland May 13 '15 at 16:12
  • is the config file on a network drive or your local machine? – DrewJordan May 13 '15 at 16:24
  • @DrewJordan It is on a shared drive with a VirtualBox virtual machine. I will try to do a test on a local disk. – Roland May 13 '15 at 16:36
  • @Roland i tried in my local system and above code is working fine. – Sachu May 13 '15 at 16:36
  • yeah, this said the same thing, although it doesn't mention a fix: http://stackoverflow.com/questions/29120361/configurationmanager-save-fails – DrewJordan May 13 '15 at 16:42
  • @Sachu I tried to run without the VS debugger, and then there is no exception. But the new value is also not saved in App.config. Did you manage to save a new value? – Roland May 13 '15 at 16:44
  • I am testing now in a local dir and without debugger, and there is no exception. But also no updated app.config. In fact, I did a clean rebuild and in bin/debug there are all kinds of files, and an .exe, but no app.config. Still, I am able to get config key/value pairs (not in the above code). So if I don't see App.config, where does it want to save updates??? – Roland May 13 '15 at 16:53
  • @Roland u r write it is not getting saved.. look [link](http://stackoverflow.com/questions/13871114/writing-key-value-pairs-into-app-config-file) and [link](http://stackoverflow.com/questions/1357240/change-the-value-in-app-config-file-dynamically) hope it helps – Sachu May 13 '15 at 17:02
  • @Roland I posted an answer that should address the problems you were having with finding the changes to the config file. What makes you think that you need a custom xml file? Are you trying to save settings that are shared between users? – DrewJordan May 13 '15 at 19:13
  • @DrewJordan Thanks for your answer. Due to holiday / weekend I will try it next Monday or Tuesday. As for your question: this app is for a single-user computer. – Roland May 14 '15 at 22:38
  • np. If you're using ClickOnce, it *does* install separately for each user, but in most cases that's what you want anyhow. No need to use a custom xml file; you can use app.config here. You may also want to have a look at `Properties.Settings` as you might run into issues writing to the application's directory if the user isn't an admin: https://msdn.microsoft.com/en-us/library/bb397755(v=vs.110).aspx – DrewJordan May 14 '15 at 22:50

1 Answers1

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.

Community
  • 1
  • 1
DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • if you could add your remark about the shared disk to your solution, I would be glad to accept it. – Roland May 18 '15 at 13:20