none
ConfigurationManager.AppSettings return null when open config file using OpenMappedExeConfiguration RRS feed

  • Question

  • For some reason, I don't use App.Config but open configuration file from specified location using OpenMappedExeConfiguration.
    Program loads configuration file successfully but fails when get the value of AppSettings value.
    Here is the code snippet. 

    string config = @"c:\MyAppTest\test.cfg"; Configuration configfile; ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = configfile; configobj = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); //I checked the configobj.FilePath is correctly acquired! string strlog4net = ConfigurationManager.AppSettings["log4net.config"];//Error occur here because

    //ConfigurationManager.AppSettings["log4net.config"]

    //is null!

    Can anybody help me to resolve this problem?





    • Edited by Jeff0803 Tuesday, August 27, 2019 8:41 PM
    Tuesday, August 27, 2019 7:08 PM

Answers

  • Hi Jeff0803,

    Thank you for posting here.

    If you want to read external config file, I don't suggest that you use Configuration to read it. You could use XDocument Class to do it.

    I write a simple code, you could have a look.

                string path="D:\\test.config";
                string txt = File.ReadAllText(path);
                XDocument doc = XDocument.Parse(txt);
                var result = doc.Descendants("add").Attributes("key").First();
                var mn = result.Value;
                Console.WriteLine(mn);

    Result:

    Config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>     
        <add key="MachineID" value="App2.1 Long"/>   
      </appSettings>
    
    </configuration>

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, August 28, 2019 5:13 AM
    Moderator
  • I think you misunderstand what ConfigurationManager does. ConfigurationManager is the default, singleton used to back the current application's configuration file. There is nothing you can do to change this.

    If you open your own config file using OpenMappedExeConfiguration (or equivalent) then you get back a Configuration object which is specific to your config. You must use this object in all your calls to retrieve from it. ConfigurationManager effectively does this same thing under the hood but it's Configuration is pointing to the app config. Again, you cannot change that. So to work with your custom config you cannot use ConfigurationManager's static methods as they use the app config only.

    //Retrieve the app settings defined in the custom config file you opened
    configobj.AppSettings["log4net.config"];
    
    //ALWAYS, pulls from the app's config file - cannot change this
    ConfigurationManager.AppSettings["log4net.config"];


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, September 4, 2019 1:55 PM
    Moderator

All replies

  • Hi Jeff0803,

    Thank you for posting here.

    If you want to read external config file, I don't suggest that you use Configuration to read it. You could use XDocument Class to do it.

    I write a simple code, you could have a look.

                string path="D:\\test.config";
                string txt = File.ReadAllText(path);
                XDocument doc = XDocument.Parse(txt);
                var result = doc.Descendants("add").Attributes("key").First();
                var mn = result.Value;
                Console.WriteLine(mn);

    Result:

    Config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>     
        <add key="MachineID" value="App2.1 Long"/>   
      </appSettings>
    
    </configuration>

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, August 28, 2019 5:13 AM
    Moderator
  • Hi Jack,

    The OpenMappedExeConfiguration() is supposed to "Opens the specified client configuration file".

    Is there any reason that avoid Configuration? I referenced MS Document.

    We are using complex config file dynamically and using class inherited from ConfigurationSection.

    If I use XDocument, I have no idea how to use existing class concurrently?

    Do you think this is a Microsoft bug?

    Is there any other workaround?

    FYI, we are using .NET Framework 2.0.


    • Edited by Jeff0803 Wednesday, August 28, 2019 1:44 PM
    Wednesday, August 28, 2019 1:24 PM
  • Hi Jeff0803,

    Thanks for the feedback.

    Based on my research, I could find a way to read external config. Therefore, I have two suggestions.

    First, I suggest that you could put your config in the current path.

    Second, you could use the above method to read xml file.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 29, 2019 6:25 AM
    Moderator
  • I think you misunderstand what ConfigurationManager does. ConfigurationManager is the default, singleton used to back the current application's configuration file. There is nothing you can do to change this.

    If you open your own config file using OpenMappedExeConfiguration (or equivalent) then you get back a Configuration object which is specific to your config. You must use this object in all your calls to retrieve from it. ConfigurationManager effectively does this same thing under the hood but it's Configuration is pointing to the app config. Again, you cannot change that. So to work with your custom config you cannot use ConfigurationManager's static methods as they use the app config only.

    //Retrieve the app settings defined in the custom config file you opened
    configobj.AppSettings["log4net.config"];
    
    //ALWAYS, pulls from the app's config file - cannot change this
    ConfigurationManager.AppSettings["log4net.config"];


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, September 4, 2019 1:55 PM
    Moderator