通过传递.config文件的路径c#将键/值对读取到字典

Mr.*_*oxy 1 c# linq settings config

我有两个配置文件,旧版本和最新版本.我需要更新旧的,所以我制作了以下代码来打开它们并将键/值加载到两个字典中,我将在稍后进行比较.代码如下:需要改变什么?

 public void UpdateCliente(string FilePathOld, string FilePathNew)
 {
      Dictionary<string, string> Old = new Dictionary<string, string>();
      Dictionary<string, string> New = new Dictionary<string, string>();
      List<string> KeysOld = new List<string>();
      List<string> KeysNew = new List<string>();
      //Keys = ConfigurationSettings.AppSettings.AllKeys.ToList();

      ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
      configMap.ExeConfigFilename = FilePathOld;
      Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
       KeysOld = config.AppSettings.Settings.AllKeys.ToList();

       Old = (config.GetSection("<appSettings>") as System.Collections.Hashtable)
                .Cast<System.Collections.DictionaryEntry>()
                .ToDictionary(n => n.Key.ToString(), n => n.Value.ToString());

      //Old = (config.GetSection("<appSettings>") as System.Collections.Hashtable)

  }
Run Code Online (Sandbox Code Playgroud)

这一行: Old = (config.GetSection("<appSettings>") as System.Collections.Hashtable)给我以下错误:

无法通过引用转换,装箱转换,拆箱转换,换行转换或空类型转换将类型'System.Configuration.ConfigurationSection'转换为'System.Collections.Hashtable'

注意:我忘记了转换新文件的密钥的代码,但方法应该是相同的!

Mik*_*ala 6

你的意思是,例如......

Configuration config = 
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

KeyValueConfigurationCollection settings = 
    config.AppSettings.Settings;

Dictionary<string, string> dictionary = 
    settings.AllKeys.ToDictionary(key => key, key => settings[key].Value);
Run Code Online (Sandbox Code Playgroud)

此外,我认为应该是 config.GetSection("appSettings")