none
Open an app.config file using ConfigurationManager in .NET 4.0

    Question

  • I am using the following statement in a console application:

    System.Configuration.

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    It appears to be opening ConsolApp1.vshost.exe.config instead of ConsolApp1.exe.config.

    The reference to System.configuration checks out as valid in the object browser.

    The project is hosted in vS2010.

    What am I doing wrong?


    jim...
    Monday, January 24, 2011 11:19 PM

Answers

  • I finally got the post from a post on Stack Overflow.

    What I had not thought through was that I was writing a DLL not and EXE.

    (It was for Visual Studio Tools for Office where the main application was

    Excel and all my code was packaged into DLLs.)

    There are many good reasons why DLLs should not have Configuration files.

    jim...


    jim...
    • Marked as answer by Luvs2Code Friday, February 18, 2011 10:43 PM
    Friday, February 18, 2011 10:43 PM

All replies

  • You're probably doing nothing wrong. When running an application from using the Visual Studio Debugger, the file that actually executes is ConsolApp1.vshost.exe and it uses the ConsolApp1.vshost.exe.config file. This config file should be exactly the same as the normal ConsolApp1.exe.config. The debugger uses theses vshost files to improve debugging or something. When running without the debugger (Ctrl + F5) the normal exe and config files are used. 

    If you're having any troubles they shouldn't be caused by the vshost stuff. Then it's most likely something else.


    Regards, Simon
    Tuesday, January 25, 2011 6:48 AM
  • Hi Luvs2Code,

    Thank you for posting.

    In order to accelerate creating process when debugging, Visual Studio keeps appname.vshost.exe always running when we develop our applications. Right after we launch the debugger, it will communicate with this process. Then, the actual .exe file will be loaded into an isolated application domain and execute code there. The vshost.exe.config file is the file where .net CLR will look for the config information of the program.

    Please see this MSDN blog for more information:

    http://blogs.msdn.com/b/dtemp/archive/2004/08/17/215764.aspx

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 25, 2011 11:38 AM
  • Hi,

    Any update?

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 03, 2011 3:15 AM
  • No update. I hope to revisit it this weekend. As of now, I am convinced that ConfigurationManager is opening the VSHOST configuration file instead of the [programname].exe.config.

    When I use the former way of opening config files, the compiler says that method is obsolete and to use configuration manager.

    I hope I don't have to write a function to open the file manually and to parse the <appSettings> subsection.

     


    jim...
    • Marked as answer by Luvs2Code Friday, February 18, 2011 10:40 PM
    • Unmarked as answer by Luvs2Code Friday, February 18, 2011 10:40 PM
    Thursday, February 03, 2011 1:30 PM
  • I finally got the post from a post on Stack Overflow.

    What I had not thought through was that I was writing a DLL not and EXE.

    (It was for Visual Studio Tools for Office where the main application was

    Excel and all my code was packaged into DLLs.)

    There are many good reasons why DLLs should not have Configuration files.

    jim...


    jim...
    • Marked as answer by Luvs2Code Friday, February 18, 2011 10:43 PM
    Friday, February 18, 2011 10:43 PM
  • What if you actually want to use the configuration file as a configuration file?  In my application the user needs to edit the configuration file.  The application will create a dummy configuration file if it does not exist.  When I run in the debug mode the vshost configuration file is deleted when it closes.  If you save a copy of the real configuration file with the app.vshost.exe.config name, it will be deleted when the debug interface starts.

    To get around this I use the precompiler option as follows:

    #if DEBUG

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("AppName.exe");

    #else

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    #endif

    This will allow you to view the configuration file when debugging.


    Scaling the Cliffs of Insanity
    • Edited by Maineac Monday, April 25, 2011 3:57 PM code segment was in-complete
    Monday, April 25, 2011 3:53 PM