0

I want to save new values to settings in Windows Forms. I looked up the other posts on this topic, and tried using code from the answers there, but to no avail.

Here's my code:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["DatabasePassword"].Value = textBoxPassword.Text;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

According to this post, and various others, this code should change the values in the exe.config file at the very least. But App.config, .exe.config, and .vhost.exe.config are all unchanged. Why is this not working?

I should also mention that this code is inside a buttonClick event.

Update: I've managed to get a change in .vhost.exe.config by using a .Remove(key) and .Add(key, value) , though this does inconveniently rearrange the document so that the new key is at the bottom, and on top of that, the .vhost.exe settings appear to have no effect. The only configuration that the program seems to draw data from is App.Config. I assume I'll need some special code to specifically read from the .vhost.exe.config instead?

Community
  • 1
  • 1
Eliezer Miron
  • 388
  • 5
  • 18
  • Strange it works on my machine when key exists in file. Can you tell something more - what happens? File simply is not updated? Any exception or something more? – Krzysztof Madej Aug 14 '15 at 17:47
  • take a look here for an example http://stackoverflow.com/questions/5274829/configurationmanager-appsettings-how-to-modify-and-save – MethodMan Aug 14 '15 at 17:51
  • No exceptions, not even any noticeable response, besides the button click event. The button changes text after it does all of that with the config, so I know it got through the entire event without any errors – Eliezer Miron Aug 14 '15 at 17:53
  • One more question - Are you sure event was trigerred? – Krzysztof Madej Aug 14 '15 at 17:54
  • Yes, I'm sure this code is run, as I said, the other methods in the event (which come after this) are run. Would having multiple lines of "config.AppSettings.Settings["DatabasePassword"].Value = textBoxPassword.Text;" change anything? I have one for each setting. – Eliezer Miron Aug 14 '15 at 17:58

0 Answers0