Link to home
Start Free TrialLog in
Avatar of sej69
sej69

asked on

Storing and accessing user config files in C#

I'm got an application that I started out using the registry to store the config files then discovered that isn't the best idea with UAC and moved to app.config.  Problem is, that seems to be a big issue with UAC and user rights to the program files folder.  What is everyone else using to store user application config information? Oh also, this needs to be store by MACHINE not by user....  All users that log on that computer need to operate the same configuration. Thanks!
Avatar of Vel Eous
Vel Eous

Take a look at the following Enum list, specifically the CommonApplicationData member:

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

ASKER

Very interesting list!  However, UAC still prevents file access to all the common area folders...
ASKER CERTIFIED SOLUTION
Avatar of Vel Eous
Vel Eous

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Todd Gerbert
If your application genuinely requires that all users share the same set of parameters (I would carefully consider this requirement, since doing so kinda defeats the point of having separate user accounts to some extent), then you have a couple options.

1) You can create a new folder off the root of C:, C:\YourProgramData for example, during installation and store settings there. I'm not 100% sure, but I believe that UAC won't be such a pain in the butt in this case (provided NTFS permissions are set correctly).

2) Continue using the registry and accept UAC as a fact of life.

3) Use app.config in C:\Program Files\YourApp to store settings, and again you'll have to accept you'll need to work with UAC. This would be my choice.

You can include a UAC manifest with your application indicating that administrative privileges are required to run it, so users will receive the UAC prompt when your application starts up; or, you can instruct users that if they wish to be able make changes to the settings they'll need to right-click the shortcut and choose "Run as Administrator..."

Otherwise, you can use P/Invoke with Windows API functions to request an elevated token only when needed; e.g. your program runs fine as a normal, non-elevated user until they click the "Save" button in the settings dialog.
There are 2 kinds of configurations. User and Application. You can set each setting to either in the Settings tab when you create them.

Application settings are recorded in the application directory, which is usually restricted by UAC, and are thus readonly.

User settings however are moved to the user AppData directory when the application is first run by a given user. This directory is available to the user, so those settings are read-write.

This makes sense, because if the user changes a read-write setting, that might frustates all the other users that would like it to be set another.

So the solution is simply to decide on each user if it will be fixed and shared by everyone (Application setting), or if each user will be able to change it the way he wants (User setting).

If you want a given user to be able to change an application setting, you can always have an admin version of the application that is started as an admin and that can change the value in the xml file that is used for the configuration.
Avatar of sej69

ASKER

Thanks!