none
Multiple App.configs RRS feed

  • Question

  • Hi,

    I have my entry point application that has its own app.config and a class library. For various reasons I cannot add settings etc to that config which is why i have create a second app.config in my class library. 

    When I try to access it using configurationManager I end up in the entry applications app.config.

    Can someone please let me know how I can access the app.config in my class library?

    Thanks.

    • Moved by Min ZhuModerator Thursday, December 9, 2010 5:43 AM (From:Windows Presentation Foundation (WPF))
    • Changed type eryang Thursday, December 9, 2010 7:44 AM question
    Wednesday, December 8, 2010 11:27 AM

Answers

  •  

    Hi,

     

    You may use ConfigurationManager.OpenExeConfiguration method to open another config file for your dll, for example, there  are two projects as following:

     

    ClassLibrary1

    App.config

    Class1.cs

     

    ConsoleApplication1

    App.config

    Program.cs

     

    In Class1.cs, paste following code snippet:

     

            public static void ReadConfig()

            {

                string value = ConfigurationManager.AppSettings["key1"];

                Console.WriteLine(value);

     

                Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

                Console.WriteLine(dllConfig.AppSettings.Settings["key1"].Value);

            }

     

    Set build output path to ../Debug/ for the two project.

    Build the two projects, in the Debug folder, there are:

    ClassLibrary1.dll

    ClassLibrary1.dll.config

    ConsoleApplication1.exe

    ConsoleApplication1.exe.config

     

    Run the ConsoleApplication1.exe, you will see that the ReadConfig method shows configuration of ClassLibrary1.dll.config.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by eryang Thursday, December 16, 2010 9:54 AM
    Thursday, December 9, 2010 9:21 AM

All replies

  • Hi Pritesh3,

    I have moved your post from Windows Presentation Foundation(WPF) forum to Common Language Runtime forum where you could get better and quicker support.

    Thanks for your understanding!

    Best regards,

    Min

    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact
    msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to Microsoft All-In-One Code Framework to download or request code samples from Microsoft Community Team!
    Thursday, December 9, 2010 5:43 AM
    Moderator
  •  

    Hi,

     

    You may use ConfigurationManager.OpenExeConfiguration method to open another config file for your dll, for example, there  are two projects as following:

     

    ClassLibrary1

    App.config

    Class1.cs

     

    ConsoleApplication1

    App.config

    Program.cs

     

    In Class1.cs, paste following code snippet:

     

            public static void ReadConfig()

            {

                string value = ConfigurationManager.AppSettings["key1"];

                Console.WriteLine(value);

     

                Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

                Console.WriteLine(dllConfig.AppSettings.Settings["key1"].Value);

            }

     

    Set build output path to ../Debug/ for the two project.

    Build the two projects, in the Debug folder, there are:

    ClassLibrary1.dll

    ClassLibrary1.dll.config

    ConsoleApplication1.exe

    ConsoleApplication1.exe.config

     

    Run the ConsoleApplication1.exe, you will see that the ReadConfig method shows configuration of ClassLibrary1.dll.config.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by eryang Thursday, December 16, 2010 9:54 AM
    Thursday, December 9, 2010 9:21 AM