Search code examples
c#visual-studioconfigconfigurationmanagerapp-config

use ConfigurationManager to store user values in a file that is separate from the app config


Ok, I've just about given up on this.

I would like to be able to record user preferences using a user config file, which would be referenced from the app config file. I am trying to do this with ConfigurationManager and an app config file. I can read just fine from the user settings, but setting them is a whole other problem. I would like to keep the app settings separate from the user settings in two different files.



When I use this:

<appSettings file="user.config">
  </appSettings>

and user.config looks like:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="localSetting" value="localVal"/>
</appSettings>

I can use ConfigurationManager to READ the local setting, but not to save to the file.

var oldLocVal = ConfigurationManager.AppSettings["localSetting"]; 
ConfigurationManager.AppSettings.Set("localSetting", "newLocalValue"); // Doesn't save to file.



If instead my user.config file is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="localSetting" value="localVal"/>
  </appSettings>
</configuration>

I would then like to call and save the AppSettings in this way:

var uMap = new ConfigurationFileMap("user.config");
var uConfig = ConfigurationManager.OpenMappedMachineConfiguration(uMap);
var oldLocalVarFromConfig = uConfig.AppSettings.Settings["localSetting"]; // NO
uConfig.AppSettings.Settings.Remove("localSetting");                      // NO
uConfig.AppSettings.Settings.Add("localSetting", "newValue");
uConfig.Save();

but it won't let me access the configuration's app settings. (It has a problem casting something as an AppSettings)



I also tried with configSource instead of file attributes in the app config appSettings element.

I was using the examples here for help, but unfortunately it wasn't enough.

Thanks in advance.


Solution

  • You can load external config files into a 'Configuration' instance. Here is an example of a singleton class with a static constructor that uses this strategy. You can tweak it a bit to do what you want, I think.

      private const string _path = @"E:\WhateverPath\User.config"
    
      static ConfigManager()        
        { 
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()          
            { 
                ExeConfigFilename = _path
            }; 
    
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    
            // get some custom configuration section
            _someConfigSection = config.GetSection("SomeSection") as SomeSection; 
    
            // or just get app settings 
            _appSettingSection = config.AppSettings; 
        }
    

    Maybe try adding this way, making sure to call Save()

    _appSettingSection.Settings.Add("SomeKey", "SomeValue"); 
    
    //make sure to call save
    config.Save();