gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:00 27 4
gpt4 key购买 nike

我有两个配置文件,旧版本和最新版本。我需要更新旧的,所以我编写了以下代码来打开两者并将键/值加载到我稍后将比较的两个词典中。代码如下:需要改变什么?

 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)

}

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

Cannot convert type 'System.Configuration.ConfigurationSection' to 'System.Collections.Hashtable' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

NOTE: I forgot the code to convert the keys of the newer file but the method should be the same!

最佳答案

你是说,例如……

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

KeyValueConfigurationCollection settings =
config.AppSettings.Settings;

Dictionary<string, string> dictionary =
settings.AllKeys.ToDictionary(key => key, key => settings[key].Value);

另外,我觉得应该是config.GetSection("appSettings")

关于c# - 通过传递 .config 文件的路径将键/值对读取到字典中 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com