Storing and accessing user config files in C#

Posted on 2012-08-24
Last Modified: 2012-08-28
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!
Question by:sej69
    LVL 14

    Expert Comment

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

    Author Comment

    Very interesting list!  However, UAC still prevents file access to all the common area folders...
    LVL 14

    Accepted Solution

    Very interesting list!  However, UAC still prevents file access to all the common area folders...


    Open in new window

    Unless you have some obscure policies, your user will be allowed access to modify files within some of those directories irrespective of your UAC setting level.
    LVL 33

    Expert Comment

    by: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.
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    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.

    Author Closing Comment


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now