Pergunta

I have the following problem trying to use a custom config section in my app.config. I use a custom config section to keep track of a selection of folders that I want to back up with my program like so:

<CustomConfigSection>
    <BackupLocations>
        <clear />
        <add path="C:\Users\Marcel\Documents\" />
    </BackupLocations>
</CustomConfigSection>

Now, whenever I save the configuration file I get this exception:

System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Access to the path C: \ Program Files (x86) \ Backup Solutions \ uqhuxi1j.tmp is denied. (C: \ Program Files (x86) \ Backup Solutions \ BS.exe.Config) ---> System.UnauthorizedAccessException

The code I use is:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
CustomConfigSection section = (CustomConfigSection)config.GetSection("CustomConfigSection");

section.BackupLocations.Add(element);

section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);

This works fine when logged in as admin and only occurs when there are multiple accounts configured on a PC, so this means it is a UAC/permissions problem. As a regular user I do not have the proper write permissions for that folder.

Now for my question(s):

How come the user settings get saved to AppData and the Custom config section is trying to save in the ApplicationFolder/exe.config? How can I make my custom section save to the AppData config file too?

I need to be able to save my data for every user, regardless of permissions. So can I achieve this using a custom config section or do I need a different approach? Preferably I would like to save my custom section into the AppData config file as well. I do not want to use tricks in my Installer to adjust permissions to my application folder to allow it to write. Most of all I do not want to require administrator permissions when starting up my program!

Thanks for your replies, much appreciated.

Foi útil?

Solução 2

After a good nights sleep I seem to have stumbled upon the answer already myself.

The config is being saved in the AppData Directory for version and user specific data. What I was doing was actually directly opening the exe.config and not doing anything regarding user-scoped settings, let alone writing to the AppData config file for my program's current version. When I realised this, I knew what to look for on google.

I found these articles:

Windows Forms - Creating and Persisting Custom User Settings in C#

Windows Forms - Creating and Persisting Custom User Settings in C# - Part 2

Experimenting and downloading the source files of that project helped me out a lot and provided me exactly with what I need. So basicly I was implementing the wrong functions for what I wanted to do.

Thanks to the readers and thanks to the Mr. Ritchie for somewhat sending me into the right direction.

Outras dicas

User-scoped settings get saved to AppData because users have permission to write there. Users don't have permission to write to Program Files. Use User-scoped settings in order to save them to AppData. See http://msdn.microsoft.com/en-us/library/a65txexh(v=vs.100).aspx for more details.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top