5

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;
    }
}
Community
  • 1
  • 1
McPolu
  • 43
  • 1
  • 11
  • 2
    You have the answer here: http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location – Aleksandar Vucetic Feb 11 '12 at 01:19
  • @vucetica: The tricky bit is that configuration.GetSection(sectionName) returns a System.Configuration.DefaultSection, going from an instance of that class to a NameValueCollection or IDictionary was the missing part in all the answers to similar questions, blogs, articles, etc. You have to do the xxxHandler(null, null, XmlDocument.DocumentElement) figure skating trick. – McPolu Feb 11 '12 at 01:25
  • 2
    @vucetica have the answer? Whats the *question*? – Chris Shain Feb 11 '12 at 01:34
  • I don't know what's the question then. What do you want to achieve? – Aleksandar Vucetic Feb 11 '12 at 01:39
  • Erm... no question, really. Just had a problem, googled for a soution, didn't find it, scratched my head until I figured it out myself and decided to post it here so the next guy can find it. Sorry if it goes against the etiquette of this place to post a "question" that includes its own answer, it is the first time I post something here. Stackoverflow is usually among the top 10 google results for programming topics, I have found the solution to countless problems here so I thought I would give something back. – McPolu Feb 11 '12 at 01:44
  • 1
    Appreciated- just that the normal method is to post your question and then post an answer and accept it. – Chris Shain Feb 11 '12 at 01:47
  • @user1177654: Welcome to Stack Overflow! You can post your own answer, but you probably have to wait a while. – Gabe Feb 11 '12 at 01:47
  • 1
    Throwing `System.Exception`? That object should never be thrown; it's completely meaningless. There also does not appear to be any reason for your `retVal` variable. – Andrew Barber Feb 11 '12 at 04:13

0 Answers0