Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read/write app.config settings at runtime without using user settings?

I'm looking for a way to store application or machine level settings that can be written to at runtime using Application Settings. User settings allow read/write but application settings do not. I have been using user settings for saving settings like this at runtime but this has really proven to be impractical for the following reasons:

  • All users of the machine need to share settings.
  • In support calls (especially in crisis situations) it is difficult to explain to users/employees where to find and modify these settings manually (appdata is a hidden folder among other things).
  • New versions of the app need to use previous settings (user settings seem to get blown away with new versions).
  • It is common for our employees to copy the application to a new folder which also resets the user settings.

Our company machines are only used by one user anyway so user specific settings are not generally needed.

Otherwise I really like using application settings and would like to continue to use them if possible. It would be ideal if the settings could reside in the same folder as the EXE (like good ol' ini files once did).

NOTE: This is a WPF application and not an ASP.net web app so no web.config.

like image 499
Tim Santeford Avatar asked Sep 03 '10 19:09

Tim Santeford


People also ask

How do I access Visual Studio settings?

In Visual Basic projects, you can access application settings at runtime by using the My. Settings object. On the Settings page, click the View code button to view the Settings.


1 Answers

Well, I haven't yet wanted to change application settings at runtime (that's what I use user settings for), but what I have been able to do is write application settings at install time. I imagine that a similar approach might work at runtime. You could try it out since there don't seem to be any other propsed solutions ATM.

    exePath = Path.Combine( exePath, "MyApp.exe" );
    Configuration config = ConfigurationManager.OpenExeConfiguration( exePath );
    var setting = config.AppSettings.Settings[SettingKey];
    if (setting != null)
    {
        setting.Value = newValue;
    }
    else
    {
        config.AppSettings.Settings.Add( SettingKey, newValue);
    }

    config.Save();

Hope that helps!

like image 68
Task Avatar answered Sep 17 '22 15:09

Task