Why are application settings read-only in app.config?

后端 未结 6 1008
南方客
南方客 2020-12-09 02:00

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

相关标签:
6条回答
  • 2020-12-09 02:19

    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.

    0 讨论(0)
  • 2020-12-09 02:22

    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.

    0 讨论(0)
  • 2020-12-09 02:22

    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.

    0 讨论(0)
  • 2020-12-09 02:27

    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.

    0 讨论(0)
  • 2020-12-09 02:34

    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.

    0 讨论(0)
  • 2020-12-09 02:45

    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();
    
    0 讨论(0)
提交回复
热议问题