Reading a configuration file from an abitrary path

Here a a few, but often useful, lines of code that open a .NET configuration file from any given path. Just use the OpenMappedExeConfiguration method :

var configFileMap = new ExeConfigurationFileMap();

configFileMap.ExeConfigFilename = @"C:\TEMP\MyApp.config";

var cfg = ConfigurationManager.OpenMappedExeConfiguration(configFileMap,

ConfigurationUserLevel.None);

Console.WriteLine(cfg.AppSettings.Settings["MyKey"].Value);