34

I have some settings in my app.config which I intend to be 'global' - ie. any user can change them, and all users get the same setting.

But unless I change them to be user settings, they are read only.

Why is this?

And how should I go about persisting my app's global settings?

Edit:

This is actually a windows service application which runs as a service as LocalSystem. It can also be run manually by a local admin with argument "/config", which launches a windows form to edit configuration values.

So it will have write access to %PROGRAMFILES% in both situations.

The way I am accessing my settings is thusly:

Settings.Default.MySetting = MyNewValue;

And when MySetting is set to Application (in my project properties, Settings.settings), I get a compile-time error "MySetting is read only".

I am new to this stuff, and have not yet found a very good explanation of how it is supposed to be done. For example, why do I need to say 'Default', and what does that actually mean? I have no idea. If anyone can point me to an app.config usage tutorial, that would be really helpful.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272

7 Answers7

83

The real complete answer:

The app.config settings are read-only because there are 2 types of settings:

  1. Application Settings
  2. User Settings

The first won't change unless the application publisher publishes a new version of it. The second is not stored in the app.config, but in a user.config file. In the abscence of this user.config file the app.config provides the default value.

If MySetting is a User Setting:

Settings.Default.MySetting = MyNewValue;
Settings.Default.Save();

It will create a user.config file at [User Local Settings Application Data]\[company name]\[application].exe[hash string]\[version] with the new settings, and those settings will prevail over the settings in the app.config file.

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • Hi. Can you please look at this question? http://stackoverflow.com/questions/39791332/application-creates-folder-with-in-place-for-space-in-name-when-using-proper – Pritam Nov 15 '16 at 12:13
  • There is a much cleaner solution: https://social.msdn.microsoft.com/Forums/vstudio/en-US/8d242c60-8603-4ccf-89ec-0d4740032e7a/programmatically-change-appconfig?forum=clr – OverMars Oct 10 '18 at 19:06
4

Why: Application settings are intended to be stored in the Application folder under Program Files where the user does not have write privileges.

How: There is no default support for "All Users" but you should be able to setup your own custom config file in a public folder or use a Database.

H H
  • 263,252
  • 30
  • 330
  • 514
3

Simply put: There's no location on a machine that everyone can change, unless you give privileges to do so.

There are several ways to deal with this kind of situation:

  • You can create a configuration file / some registry settings, put this in the "all users" profile and grant "Everyone" the rights to change that specific file. During installation you can automate the procedure for granting the appropiate privileges and your program can handle the rest.

  • You can leverage UAC to make sure the current user has the appropiate privileges to change a system-wide setting. This is the recommended approach but also means that not everyone can change specific settings.

  • You can use a shared database and store your settings in there.

  • ???

I would not recommend to change items in the program files directory or changing the default privileges overthere.

EDIT: As local system you have indeed write privileges to the program files directory. If you get the "Read only" error, it means the settings itself are read only. You'll need to use the configuration manager to be able to change the settings in configuration files.

Hope this helps.

Jeroen Landheer
  • 9,160
  • 2
  • 36
  • 43
1

One reason is that the app.config file is in your app's folder under the Program Files directory, and everything in Program Files is read only for standard users by default.

Another is that app.config settings apply system wide. If one user makes a change it will impact other users. Normal users are not supposed to be able to make that kind of change. Anything that can impact multiple users should only be set by a system administrator. Per-user settings belong in each user's Application Data folder.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • That's ok - I only want the system administrator to be able to change these system-wide settings from within my program. But it doesn't seem possible, even if I'm running as admin. – Blorgbeard Apr 16 '09 at 23:32
0

I'm using this code (a static method) to change the default settings:

public static bool SetGlobalSetting(string settingName, string settingValue)
{
    try
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        XmlNode xmlNode = xml.DocumentElement.SelectSingleNode("descendant::setting[@name='" + settingName + "']");
        xmlNode.SelectSingleNode("value").InnerText = settingValue;
        xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

        Settings s = new Settings();
        s.Reload();

        return true;
    } catch (Exception e)
    {
        // process the exception as you need
    }
        return false;
    }
}
0

Not quite sure what you mean here. Do you mean you allowed users to alter app.config from the UI and the changes are not persisted?

did you call

ConfigurationManager.RefreshSection("appSettings");

and

Configuration.Save();
oscarkuo
  • 10,431
  • 6
  • 49
  • 62
0

Configuration Settings are cached in the memory when you starts the application. you can deal with the app.config file as xml to change the values.

Khaled Musaied
  • 2,513
  • 3
  • 25
  • 38