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.