-1

I Created a windows application that uses app.confing and updates app.config when needed. When I run the application in debug mode it's working fine, but after I created the .exe file and installed it's not updating the app.confing file anymore.

I unchecked the Enable the Visual Studio hosting process to no luck.

This is the code I'm trying:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Updating the config values here 
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Jquey007
  • 65
  • 1
  • 1
  • 6
  • Are you getting any errors? Make sure you haven't got the config open in another program when running the exe. – PeteGO Dec 18 '15 at 19:44
  • Possible duplicate of [ConfigurationManager doesn't save settings](http://stackoverflow.com/questions/4216809/configurationmanager-doesnt-save-settings) – Bradley Uffner Dec 18 '15 at 19:46
  • @Bradley Uffner - Very different. This op is calling `Save` - the other wasn't. This op's code works in debug mode but not after compiled into an exe, the other post had trouble in debug mode. – PeteGO Dec 18 '15 at 19:51
  • 1
    After it's installed, is it going to Program Files? In debug mode,you have write permissions. Once installed, unless you run as admin, you may not have write permissions for that directory. – Nikki9696 Dec 18 '15 at 20:03
  • I assume it's writing to the config for the application instead of the user based on code sample. User config I believe is now stored with user profile and should be writable. Program config I believe is still in program files folder and may not be. – Nikki9696 Dec 18 '15 at 20:06

2 Answers2

1

Try calling this overload;

public void Save(
    ConfigurationSaveMode saveMode,
    bool forceSaveAll
)

passing forceSaveAll=true?

Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
1

Check if the app config is being hosted in the Program Files folder (or child folder) once installed. Since in debug mode, you're running from a location in which you have write permissions, and since once installed, application config (not user config) is often sitting in Program Files, you typically won't have write permissions. You might try running the application as an admin to see if it works in that mode, or just go examine that directory and check the write bits for your login (assuming app is running as you). Try to modify that file manually. If you can't, that's your problem.

Nikki9696
  • 6,260
  • 1
  • 28
  • 23