How do you load the config file of the currently executing assembly via the ConfigurationManager without directly referencing/loading the config file manually?

Answered How do you load the config file of the currently executing assembly via the ConfigurationManager without directly referencing/loading the config file manually?

  • Thursday, February 19, 2009 10:43 AM
     
     
     How do you load the config file of the currently executing assembly via the ConfigurationManager without directly referencing/loading the config file manually?

    I have an executable A which has a config file and some assembly B which has its own config file. Let's say class A in executable A executes a method of class B in assembly B. Calling ConfigurationManager.AppSettings["sometext"] attempts to retrieve the value from executable A's configuration file. How can I retrieve a value from assembly B's configuration file without manually loading assemblyB.dll.config (for example). The main entry assembly here is executable A.

    Thanks!

All Replies

  • Thursday, February 19, 2009 3:36 PM
    Moderator
     
     Answered Has Code

    I assume you're talking about doing this without hardcoding the assembly name into the file.  You can use a combination of Assembly.GetExecutingAssembly().Location and OpenExeConfiguration to fetch the Configuration object for your specific running assembly.  

    string path = Assembly.GetExecutingAssembly().Location;

    Configuration config = ConfigurationManager.OpenExeConfiguration(path);

    string value = config.AppSettings.Settings["value"].Value;

     


    David Morton - http://blog.davemorton.net/
    • Proposed As Answer by thomas_woelfer Thursday, February 19, 2009 4:03 PM
    • Marked As Answer by Zhi-Xin Ye Wednesday, February 25, 2009 10:59 AM
    •