Pergunta

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?

Foi útil?

Solução

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>
    
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top