1
votes

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");
2
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
@DipenShah I want to make changes permanently. Can you help me how to set file access during installation. - Abhishek Thakur
Are you using installshield to generate setup file? - Dipen Shah
@DipenShah No, Normal Install Project. - Abhishek Thakur
@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

2 Answers

1
votes

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).

1
votes

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.