adding KEYS to App.Config in VS 2005

Just got VS2005, and I am trying to use the included methods for adding keys to the app.config file. Here is my code:

System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSection.Settings.Add (new KeyValueConfigurationElement ( "NEW", "Something"));

config.AppSettings.Settings.Add(new KeyValueConfigurationElement(""NEW", "Something"));

config.Save();

it runs, but does nothing. the Keys are not added. No errors of any sort. Any ideas



Answer this question

adding KEYS to App.Config in VS 2005

  • Sirkku Willie MSFT

    I sure hope what you've got there doesn't run... AppSection.Settings no longer exists if I am not mistaken, and never used for the WinForms .config file.

    Let me guess though... you are executing the program in the IDE From the looks of it, as soon as Save() is called the config file is updated... only it gets whiped out with your default app.config file after closing. Correct

    The reason for this would be because the vshost wrapper reloads, and in doing so takes your default app.config file specified in the project and overwrites the one in your build destination, causing you to loose those settings you wrong.

    Does this make sense

    See http://msdn2.microsoft.com/en-us/library/system.configuration.appsettingssection.aspx for more info on writing to your config file.



  • mooremedia

    Yes, that is exactly what happens. While the app is running, I can add new keys, then, I can iterate through the keys and view my changes. but as soon as I exit, the app.config file gets reloaded with the original, losing all the changes. I call the save() method of the config.appsettings, but that doesnt do anything to help.. any ideas
  • Nuno Castro

    Try disabling the Visual Studio hosting process to keep the vshost from starting and overwriting your file. From your project properties, choose the Debug tab and you should find the checkbox at the bottom of the list.

    Just for note though, there are benefits of the vshost, take a look at this blog entry (http://blogs.msdn.com/dtemp/archive/2004/08/17/215764.aspx) for some info on what they are and why they can be useful.

    I hope that this solves your problem.



  • Alex Aidar

    Thanks for the help. I just ran the .exe on its own, without the debugger and everything works as expected. Thanks again.
  • adding KEYS to App.Config in VS 2005