1

I'm creating a WPF application. It works fine in Visual Studio. But, whenever I modify configuration settings through application after installation, It throws error 'Error occurred loading a configuration file: Access to path c:\Program Files (x86)\… denied'. I can't run the program as Admin every time as per requirement. Is there any way to solve this?

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");connectionStringsSection.ConnectionStrings["cn"].ConnectionString = "data source=" + txtServer.Text + ";database=" + txtDatabase.Text + ";User ID=" + txtUserID.Text + ";Password=" + pwdPassword.Password;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");

config.Save(ConfigurationSaveMode.Modified);

// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
  • Do you need to persist the changes? If yes, why don't you set file ACL during installation? or right approach would be prompt to restart app in admin mode when such a chnge is required. – Dipen Shah Feb 28 '20 at 09:56
  • @DipenShah I want to make changes permanently. Can you help me how to set file access during installation. – Abhishek Thakur Feb 28 '20 at 09:57
  • Are you using installshield to generate setup file? – Dipen Shah Feb 28 '20 at 09:58
  • @DipenShah No, Normal Install Project. – Abhishek Thakur Feb 28 '20 at 09:58
  • Can you [relocate your app config](https://stackoverflow.com/a/27532456/3754386) to a different directory (like %appdata% or any other user allowed folder)? – Fixation Feb 28 '20 at 09:59
  • @AbhishekThakur I haven't used VS Install Project for a while now, but I believe you should be able to use custom action and use File.SetAccessControl afterwards. https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/d9k65z2d%28v%3dvs.100%29 https://learn.microsoft.com/en-us/dotnet/api/system.io.file.setaccesscontrol?view=netframework-4.8 – Dipen Shah Feb 28 '20 at 10:07
  • 1
    @DipenShah I tried reallocating app.config to a different location. I wish it works. Also, will try File.SetAccessControl if above mentioned fix didn't work. Thanks – Abhishek Thakur Feb 28 '20 at 10:10
  • @AbhishekThakur I wouldn't be sure about moving app config to a different folder, you can technically create a separate config file and load it instead of using app.config. I believe, app.config will be required by exe package for information about related assembly. – Dipen Shah Feb 28 '20 at 10:13

2 Answers2

1

UAC will stop a user from changing anything inside program files. This is what is raising your error.

https://social.technet.microsoft.com/wiki/contents/articles/30915.c-local-files.aspx

Your alternatives include:

Explicitly change the authorisation on the folder the config is deployed to. This is widely considered very bad practice. Forget this is you distribute to external clients.

Instead of using config file for these variables, write them to a file in appdata instead. There is a public appdata you could use if this is a multi user install or you can use the user's appdata.

The latter is the approach I would recommend.

Build a class which is suitable to hold your data.

Serialise this to a string and save as an xml or json file into appdata.

Andy
  • 11,864
  • 2
  • 17
  • 20
1

Rule of thumb: Treat your installation folder under program files as read-only.

That is really all there is to it. I have this list of approaches to deal with access denied. Most options there are bad and listed so one can remember why they are bad (I just updated the linked answer with color to indicate more recommended approaches - it is a bit messy, but at least it can spark ideas).

Go for move file to writable location, or use internal defaults only, or better yet read from the cloud (database) and write to userprofile if you need to.

The traditional way is obviously the registry with HKCU entries.

(I like the "versioning and backup of settings" made possible by database storage of settings as opposed to just settings files - more work though).

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164