简体   繁体   中英

C# app.config files and UAC

I started out with my application writing the config to the registry like I did years back with UAC wasn't around.

Turned out this wasn't a good idea so I moved to the app.config file and found that this is having issues as well with UAC.

The configuration I have for the application is machine specific; individual users do not have their own configuration and the rest of the apps including the service all drive off the single configuration file.

If I try updating this file in the program files\\myapp folder it gets denied.

What is everyone else doing for a scenario like this where there needs to be a single configuration file?

I should also mention that this configuration needs to be updated from within my applications.

I would use the Common Application Data folder, this is where most new applications store data which works with UAC.

You can get this with the following code:

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

To view all options for special folders you can check this link:

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

The most common options would be the following:

Application Data - The directory that serves as a common repository for application-specific data for the current roaming user.

Common Application Data - The directory that serves as a common repository for application-specific data that is used by all users.

Local Application Data - The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.

You should be storing your users' settings in app.config you should be using user.config . Have a look here for more info.

Any machine-wide configuration files should be stored in:

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

Files in Program Files shouldn't be modified except when the application is installed/updated.

I had a similar need with a WPF application. What I do is the following:

  1. Install the application with a boiler-plate configuration file.
  2. When the app runs, it checks for a config file in the user's App Datadirectory for configuration files, if they're not present it copies the default boiler-plate config files from the install directory.
  3. I then do any first-run initialization stuff that is needed.

I started out using the .NET ConfigurationManager, but I switched over to a plain old XML files because it was simpler and my requirements were basic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM