How to get application settings common to all users that can be changed at runtime

I need some customization of the application, which will be used by all users of the computer, but can also be changed at runtime. This seam is simple, but according to the MSDN application settings article, it is either one or the other.

There are two types of area-based application settings:

  • Application area parameters can be used for information, such as a web service URL or database connection strings. These values ​​are application related. Therefore, users cannot change them at run time.

  • User preferences can be used for information, such as saving the last position of a form or font preference. Users can change these values ​​at runtime.

I could write code for editing the XML file app.config, but since it is in the program directory, it is protected under windows 7. Thus, this is impossible without raising the program or playing with NTFS permissions.

Therefore, I need the configuration file to be written to a shared folder of type System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) .

But this is a fairly common requirement!

So, I am wondering if there is an easy way to achieve this without reinventing the wheel, or if I need to write my own settings manager.

+8
c # visual-studio-2010 application-settings
source share
3 answers

I had a similar problem and I wrote my own class of settings. It was very simple. I created a settings class with properties that I need, and SettingsManager methods with Save () and Load () methods that simply serialized / deserialized the object via the XmlSerializer to / from the file.

Yes, this is your own code, but it is very simple code, it takes less time than trying to figure out if there is a component that provides what you need and how to configure it.

+4
source share

After reading the answers here and playing with the ConfigurationManager.Open methods, I got the following:

 string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApp", "MyApp.config"); Configuration MyAppConfig = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = path }, ConfigurationUserLevel.None); 

The good part is a file that should not exist, and if you name Save() on it, it will create a folder and a file for you. AppSettings["key"] did not work, so I had to use

 MyAppConfig.AppSettings.Settings["key"].Value 

to read and write the existing value and

 MyAppConfig.AppSettings.Settings.Add("key", value) 

to add a new value.

+2
source share

The application settings infrastructure does not support this - only non-editable application data and data with user scope are supported. However, you can easily read and write your own XML in the CommonApplicationData folders, instead of using application data.

+1
source share

All Articles