Possible Duplicate:
Using ConfigurationManager to load config from an arbitrary location
It took me a couple of hours and a sneak peek into C# framework classes to figure it out. There are a few questions out there about how to do it but no definitive answer. The best link I could find was C# DLL config file which covers exactly my use case, a .dll reading from a global .config file regardless of which process is hosting it.
You could use reflection to create the handler object but it would make it less readable, this is not production code, just a snippet to show how it works. I've kept it as simple and clear as possible.
public class ConfigurationManager
{
public static object GetSection(string sectionName)
{
var configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(
new System.Configuration.ConfigurationFileMap(
@"X:\path\to\your\file.config"));
object retVal;
var section = configuration.GetSection(sectionName);
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(section.SectionInformation.GetRawXml());
if (section.SectionInformation.Type.Equals("System.Configuration.NameValueSectionHandler"))
{
var handler = new System.Configuration.NameValueSectionHandler();
retVal = handler.Create(null, null, xmlDoc.DocumentElement);
}
else if (section.SectionInformation.Type.Equals("System.Configuration.DictionarySectionHandler"))
{
var handler = new System.Configuration.DictionarySectionHandler();
retVal = handler.Create(null, null, xmlDoc.DocumentElement);
}
else
{
throw new System.Exception("Unknown type " + section.SectionInformation.Type);
}
return retVal;
}
}