Home > Enterprise >  C# - Unable to write to app.config after deployment
C# - Unable to write to app.config after deployment

Time:10-06

I am developing a WPF application which runs well in visual studio v.2022 very well. My problem is when I deploy the application to another computer for testing it does not seem to be updating app.config data. When I change the ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) to NONE it works well in both debug and release mode in Visual Studio but throws an error when deployed to another computer. The error I am getting is "System configuration.configuration error exception - An error ocurred while loading the the configuration file myApp.dll.config. Access denied " I gather that the user has no write privileges on the Program Files folder where the app.config is stored. However, when I change the user level to either Roaming or Roaming and local, then nothing happens as the configuration file is not located and if at all the changes are made then they are not persisting. Here is my app.config file

<configuration>
   <appSettings>
      <add key="Server" value=""/>
      <add key="Port" value=""/>
      <add key="Database" value=""/>
      <add key="User" value=""/>
      <add key="Pwd" value=""/>
      <add key="Code" value=""/>
      <add key="Access" value=""/>
      <add key="Status" value=""/>
      <add key="EndDate" value=""/>
   </appSettings>
</configuration>

Here is my method for updating the the app.config

 public static void UpdateSetting(string key, string value)
    {
        Configuration roaming = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = roaming.FilePath;
        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        var appSettings = configuration.AppSettings;

        foreach (var keys in appSettings.Settings.AllKeys.Where(x => x.StartsWith(key)))
        {
            appSettings.Settings[keys].Value = value;

        }
        configuration.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }

and here is an example of how the method is called Settings.UpdateSetting("Code", _Code);

Any help will be much appreciated

CodePudding user response:

Following @Joe's comment it occurred to me that it was not possible to write to config application data at runtime. I opted to create a new section in the config file in the Properties.Settings folder in my solution and assigned the scope to User and not application since the user is able to update the data at runtime. Double click on the settings.Settings file to open in designer view and literally entered the settings names to be stored. The stored settings can be saved and retrieved using the methods below.Properties.Settings.Default.yourSetting. to get the settings.

And to store/save the settings

Properties.Settings.Default.yourSetting= val;`
Properties.Settings.Default.Save();`

That solved my problem.

  • Related