Configuration.Save operation not working for some machine - c#

I have a C# executable reading and updating it's configuration file (app.exe.config) at runtime, specifically, the "appSettings" section. After I make two changes to two key values and save them using the Configuration.Save() method, an "access to the path" error is thrown, but only on some users' machines (so far 2 reports out of 10,000). We have never seen this issue in-house, only in production.
The Configuration.Save() method seems to be the issue based on our program's log file. When a problem with this call happens a "ConfigurationErrorsException" occurs because the configuration file could not be written to -or- the configuration file has changed. The configuration file is in the same directory as the application. This is a Dot Net 4 application running on a Windows 7 PC.
The code is:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["Last Updated Package"].Value = packageVersion;
configuration.AppSettings.Settings["Copy Updated Files"].Value = bool.TrueString;
configuration.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");
The error is:
An error occurred loading a configuration file: Acces to the path 'C:\Program Files (x86)\My Program\bin\guaixcax.tmp' is denied. (C:\Program Files (x86)\My Program\bin\MyProgram.exe.Config)
Is this a permissions issue, or can I add something to either the code or config file to fix this issue?

I have had similar issues with .NET program installations. It has always been a permissions issue. To test if this is your problem, you should try running your application as an unprivileged user - developers tend to have administrator rights...whenever they can manage it.
If it fails, edit the security of the config file (as an administrator) to allow the "Users" group write access to your "bin" folder, and try it again. If it works, you've identified the problem. Then you can figure out what the best solution is. Some options would be:
Set access rights to necessary files/folders from your installer to work for the users that will run the application
Run the application as a user with admin rights (NOT a good idea from a security best-practices point of view)
Instead of modifying the main app settings file, create a separate settings section referencing a file that has the data that can be changed:
<configuration>
<configSections>
...
<section name="MyChangableSettings" type="My.Namespace, ChangableSettingsClass"/>
</configSection>
...
<MyChangableSettings configSource="Path\To\Writable\StorageDir\mySettings.config"/>
</configuration>

Related

C# Move AppSettings from .exe.config in Program Folder to Custom Location

C# MVVM project targeting .NET Framework 4.6.2 with underlying SQL Server database.
We have a handful of user-writable settings in appSettings. These are stored in the PROGRAMNAME.exe.config in C:\Program Files\PROGRAMNAME
The problem is that our users typically do not have write permissions to the C:\Program Files\PROGRAMNAME directory and so cannot write changes to this file. The program either needs to be run as an administrator or we need to give write permissions to that directory. Neither is ideal.
I was under the impression that it should create a copy of the .exe.config in C:\Users\USERNAME\AppData\Local\VirtualStore\Program Files\PROGRAMNAME but this does not seem to happen, especially in our Citrix environments.
Is there a way to relocate these specific appSettings to a file in a user-writable location?
You can do it. Read the config file like this:
Configuration App_Config =
ConfigurationManager.OpenExeConfiguration("C:\Temp\PROGRAMNAME.exe");
By this, it will look for the config file PROGRAMNAME.exe.config in the specified path C:\Temp
Please notice the path is not specified as C:\Temp\PROGRAMNAME.exe.config
Get more info from Here

How to copy file to the C:\Program Files (x86)\

I can copy files to the C:\Program Files (x86)\... ...but only with the administrator account.
This file is a configuration file like App.Config, but with a different name, and I want to put it into the application path's sub folder.
The scenario is like following.
Put the initial configuration file in the \Cfg\org.cfg
When start application program,myApp.exe, check if configuration file exists in the application path.
If it doesn't exist, copy org.cfg to the myApp.exe.config and load it.
I tried DirectoryInfo.SetAccessControl() but following error occur as well.
"Attempted to perform an unauthorized operation"
Program Files (x86) required administrative permission to write to. So does changing the permissions, which is why SetAccessControl fails. There's really no way around that. If you want to have your configuration file writable by the application, then you might consider changing the permission of the configuration file during installation of the program when you do have administrative access.
I would instead however recommend putting your configuration file somewhere in the user's profile, which is writable by default for all users, like in %LOCALAPPDATA%.
You can load configuration files from arbitrary locations as well, it doesn't have to be in the .config file next to the executable. For an example of that, look at this question.

Can I set a configSource for mailSettings in C:\ProgramData?

Background:
I have an application that for distribution and dependency bundling reasons is installed using Inno Setup, but at its core it's an ASP MVC 5 web app.
I'd like users to be able to install in Program Files as it's a natural place to place programs, and the average user will expect to be able to do this.
The Problem:
Some configuration files are modifiable through the app to admin users. These configuration files take the form of language strings, mail settings, etc.
The problem arises if the application is installed to Program Files. The IIS hidden user account running the application doesn't have write access to this directory, so config files inside the application directory cannot be modified by the application.
The solution I thought of was to place modifiable files inside a folder in C:\ProgramData, which is modifiable by the application. Here is where I hit an error. After changing mail settings to look like this in mt web.config
<smtp configSource="C:\ProgramData\myapp\Config\mailSettings.config" />
I get this error.
Question:
Can I not link configSource to directory outside of the application? If not, is there a way around this?
Cheers.

Application failed to start because its side-by-side configuration is incorrect

I have a c# application and I added a manifest to it and set it to an embedded resource because I didn't want it appearing alongside my exe.
I then changed my app.config file to be an embedded resource as well but my exe then didnt start up as it gave me the error:
"The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe. tool for more detail"
I have since changed it back to 'Do not copy" and I'm still getting the same error.
I have also deleted my app.config file from my project and re-added it. Not I get 2 config files:
App.config and
MyExeName.config
How can I fix this??
We found that our MyExeName.config had a comment block that was NOT CLOSED - thus making the rest of the .config file from that point onward one big comment!

Visual Studio Deployment Package Change File Permissions

Maybe this isn't possible due to Security implications...
I have a Deployment Package created in Visual Studio for a Win Form. The application references an app.config file locally and allows the user to change the settings in it, through the application.
When I deploy the application and attempt to save the changes I have made, Windows is saying "Permission denied". Is there a command I can run as part of the installation that will give the "Everyone" security permission to my app.config file....and thus allow my Application to edit it?
Thank you.
If you want to be able to write to the app.config file you need to move it to the Shared Application Data directory and modify your application to read the configuration information from there. On Win7 for example:
C:\ProgramData\YourCompany\YourApp\app.config
In C#, to get the common app data directory:
// using System.IO;
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
You might be able to use a custom action in your installer to run icacls in order give full access to Everyone:
icacls {Path to App.config} /grant Everyone:(F)

Categories

Resources