C# 读取 config 文件

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = filePath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
KeyValueConfigurationCollection keyValues = appSettings.Settings;

Dictionary<string, string> items = new Dictionary<string, string>();
 foreach (string key in keyValues.AllKeys)
{
       items.Add(key, keyValues[key].Value);
}

return items;

 

注意 :  不要使用  var 对上面程序进行 变量声明

你可能感兴趣的:(config)