Search code examples
c#wpfconfigurationmanager

Access denied when attempt Configuration.Save() for installation in Program Files


Attempts to use Configuration.Save() fail when the WPF program is installed under "Program Files". If installed elsewhere, the Save() works as expected. Isn't the program itself authorized to edit its own .config file?

I get two exceptions:

System.Configuration: An error occurred loading a configuration file: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied. (C:\Program Files\Advanced Applications\ConfigurationTest\WPF.exe.Config)
mscorlib: Access to the path 'C:\Program Files\Advanced Applications\ConfigurationTest\ejoqasrr.tmp' is denied.

I tried opening with ConfigurationUserLevel.PerUserRoaming) instead of ConfigurationUserLevel.None but it still failed, reporting different exceptions:

System.Configuration: An error occurred executing the configuration section handler for appSettings.
System.Configuration: ConfigurationSection properties cannot be edited when locked.

[NOTE: Searching found an article that appears related but didn't provide a resolution for this issue: Access to path denied for App.Config on Windows 8. How can I update App.Config?

The first two methods are called from MainVM and invoke the underlying base methods which expect to find a correlated property on the passed in object and then synchronize the appropriate values in AppSettings.

private void SettingsLoad()
{
    SettingsLoad(this);
}

private void SettingsSave()
{
    try
    {
        SettingsSave(this);
    }
    catch (Exception ex)
    {
        currentMessageAction = MessageAction.Acknowledge;
        window.MessagePanel.Show("EXCEPTION!", Utility.ParseException(ex));
    }
}
....

public static void SettingsLoad(object main)
{
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
    {
        // Attempt to get a reference to the property and, if found, set its value
        var prop = main.GetType().GetProperty(setting.Key);
        if (prop != null)
        {
            prop.SetValue(main, setting.Value);
        }
    }
}

public static void SettingsSave(object main)
{
    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    foreach (KeyValueConfigurationElement setting in config.AppSettings.Settings)
    {
        // Attempt to get a reference to the property and, if found, update its value
        var prop = main.GetType().GetProperty(setting.Key);
        if (prop != null)
        {
            setting.Value = prop.GetValue(main).ToString();
        }
    }
    // This line produces an exception
    config.Save(ConfigurationSaveMode.Modified);
}

Solution

  • Isn't the program itself authorized to edit its own .config file?

    No. The user account under which the application is running must have permissons to write to the folder/file.

    Apparently it doesn't have the appropriate permissions to write to the "Program Files" folder in your case.

    How to Set File and Folder Permissions in Windows