views:

1223

answers:

5

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.

+1  A: 

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();
oykuo
See my edit. I'm using Settings.Default, not ConfigurationManager - does it read app.config?
Blorgbeard
A: 

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 be set only a system administrator. Per-user settings belong in each user's Application Data folder.

Joel Coehoorn
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
A: 

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
+1  A: 

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
Can you expand on "use the configuration manager"? Any helpful links?
Blorgbeard
Sure, take a look here: http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx
Jeroen Landheer
+2  A: 

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