none
Problem with ConfigurationManager.OpenMappe­dExeConfiguration RRS feed

  • Question

  • Hello, I am having an odd problem with
    ConfigurationManager.OpenMappedExeConfiguration. I have the following
    code to load my config.

                var asmName =
    Assembly.GetExecutingAssembly().GetName().Name;

                var mappedExeConfig = new ExeConfigurationFileMap
                                      {
                                          ExeConfigFilename = ( asmName +
    ".exe.config" )
                                      };

                _config =
    ConfigurationManager.OpenMappedExeConfiguration(mappedExeConfig,
    ConfigurationUserLevel.None);

                if( _config == null )
                    throw new ConfigurationErrorsException("Could not find
    configuration");

    This works fine from VS and loads the config file as expected.
    However, when I run the executable from the commandline (this is a
    simple console app), it does not read the file properly. Has anybody
    ever run into something like this? The config file is in the app
    directory.

    TIA!

    rbr

    Sunday, July 18, 2010 10:33 PM

Answers

  • Hi,

    The _config always contains informations of machine.config (C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config), so _config will not be null even though the target someassebmly.exe.config doesn't exist.

    I cannot see the return statement from your code snippet, could you post a more complete source code here?


    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 rkbrown Tuesday, October 19, 2010 9:13 PM
    Tuesday, July 20, 2010 3:20 AM

All replies

  • Hi,

    What do you mean by refering to "it does not read the file properly"? it will be helpful if you could provide more information about the failure.


    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.
    Monday, July 19, 2010 6:48 AM
  • Sorry Eric. The gist is that it does not read it at all. It does not throw an error that it can't find the file. It passes over the open statement just fine but returns empty.

    Thanks,

    Ryan

    Monday, July 19, 2010 3:31 PM
  • Hi,

    The _config always contains informations of machine.config (C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config), so _config will not be null even though the target someassebmly.exe.config doesn't exist.

    I cannot see the return statement from your code snippet, could you post a more complete source code here?


    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 rkbrown Tuesday, October 19, 2010 9:13 PM
    Tuesday, July 20, 2010 3:20 AM
  • Hi Eric,

    In my case also OpenExeCongiguration() is returning null. I have done the following

    1. Create a UserControl project (VS 2010 , C# (.Net 4.0))

    2. In the control load event i ma trying to parse a configuration file (myconfig.config)

    =============================================================

    ExeConfigurationFileMap

     

     

    fileMap = new ExeConfigurationFileMap();

     

     

    if (!System.IO.File.Exists(configFileName))

    {

     

     

    throw new FileNotFoundException("Configuration file missing",configFileName);

    }

    fileMap.ExeConfigFilename = configFileName;

     

     

    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

    T myConfigSection;

     

    myConfigSection = config.GetSection(configSectionName)

     

    as T;

    =============================================================================

    Here myConfigSection is always returning null. One interesting thing is, this usercontrol i am using in one Winform application. From Winform application if i try to parse this MyConfig.config file, it is working fine.

    So my question is, is it not possible to parse a config file from inside a usercontrol or control library??

     

     

    Tuesday, October 19, 2010 10:09 AM