0

// Set LastRun to now
config.AppSettings.Settings["LastRun"].Value = DateTime.Now.ToString(); // Save all settings config.Save(ConfigurationSaveMode.Modified);

This code was working fine in my development server but not in my production server. It seems like my program is unable to communicate with my app.config file. I have checked all the "obvious" . . Any ideas ... ?

1 Answers1

2

From your code example, I cannot tell how your config variable is initialized. But, from the comments, you have a web app. Unless you are attempting to load a specific app.config file, the web app will attempt to get AppSettings from web.config.

It's not a good idea to programatically change the values of web.config. Changing web.config will cause an application restart.

If you have a different app.config for storing this type of information, that would be better than trying to change web.config. But you'll have to specifically load the file, something like this:

Configuration config = WebConfigurationManager.OpenWebConfiguration("yourPath\app.config");

ConfigurationManager.OpenExeConfiguration() is intended for use within an executable application not a web app. Try using WebConfigurationManager as shown above.

You find some more information in this SO question/answers.

More information can be found in this SO question/answer.

Community
  • 1
  • 1
dblood
  • 1,758
  • 17
  • 21
  • Ive been using : Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); . . . won't this work as well? – adicto_al_futbol Apr 02 '13 at 20:51
  • It is intended for executables. Try WebConfigurationManager. Look at those other questions I linked. – dblood Apr 02 '13 at 22:11
  • I'm getting an error: System.Configuration.configurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level: Configuration cfg = WebConfigurationManager.OpenWebConfiguration("yourPath\app.config"); String test1 = cfg.AppSettings["LastRun"]; //error – adicto_al_futbol Apr 03 '13 at 17:50
  • Look at the answer in the first SO question I linked. You'll likely have to adjust the permissions of the folder where the config file exists. – dblood Apr 03 '13 at 17:59
  • All the permissions are fine. Double checked. WebConfigurationManager isn't working. I don't know why I get an "inaccessible" error. Don't really know what else to do . . . – adicto_al_futbol Apr 04 '13 at 14:33