none
"APP_CONFIG_FILE" set to another path works only with debugger RRS feed

  • Question

  • Hi everyone!

    I have a very similar issue than this one: http://stackoverflow.com/questions/5391710/app-config-only-read-when-debugging

    I used this "common" trick (http://weblogs.asp.net/israelio/archive/2005/01/10/349825.aspx) to set a new configuration file for my executable. It works fine when I run my program with the debugger on Visual Studio 2010 (F5) but when it comes to make it without the debugger it doesn't work anymore.

    Here's the code I use: 

    AppDomain^ currentDomain = AppDomain::CurrentDomain;    currentDomain->SetData("APP_CONFIG_FILE", "PATH_TO_MY_CONFIG_FILE\\MY_FILE.xml");

    And then when I need to fetch a value from this file somewhere else in my code:

    ConfigurationManager::AppSettings["MY_KEY_IN_THE_CONFIG_FILE"]

    I tried to use the method described in the first link I gave (2nd post, he says he found the solution) but I do not succeed to have my value in the end (how to fetch the value from my file?)...

    The program throw an exception once I tried to fetch values:
    "System.NullReferenceException: Object reference not set to an instance of an object"

    If anyone can give me a hand...
    Thanks a lot !

    Wednesday, March 20, 2013 4:16 PM

Answers

  • That post is really old and not likely to work anymore.  The configuration subsystem is loaded pretty early in the process and, once loaded, it isn't going to change config settings.  Changing the config file in the manner posted is only really going to work if you're creating additional app domains or you are an app domain host.  By the time the above code is called it is pretty much too late.

    You didn't clarify why you were changing the config file path but I would strongly recommend against it.  Every .NET app works the same way so changing this fundamental behavior is going to cause problems most likely and is certainly not maintainable.  If you want to store some settings in a separate configuration file then the better approach is to use Configuration to load the custom configuration file when you need it.  You'll have all the same functionality except ConfigurationManager uses the standard config (for runtime policies, connection strings, etc) and you can use your custom configuration where needed.

    I cannot answer as to why it works in VS but not outside it.  I suspect it is just a quirk with how VS is starting the app.  I personally wouldn't rely on it.  It just seems like you're asking for trouble.  IMHO.

    Michael Taylor - 3/20/2013
    http://msmvps.com/blogs/p3net

    • Marked as answer by Grandblack Friday, March 22, 2013 10:33 AM
    Wednesday, March 20, 2013 5:03 PM
    Moderator

All replies

  • That post is really old and not likely to work anymore.  The configuration subsystem is loaded pretty early in the process and, once loaded, it isn't going to change config settings.  Changing the config file in the manner posted is only really going to work if you're creating additional app domains or you are an app domain host.  By the time the above code is called it is pretty much too late.

    You didn't clarify why you were changing the config file path but I would strongly recommend against it.  Every .NET app works the same way so changing this fundamental behavior is going to cause problems most likely and is certainly not maintainable.  If you want to store some settings in a separate configuration file then the better approach is to use Configuration to load the custom configuration file when you need it.  You'll have all the same functionality except ConfigurationManager uses the standard config (for runtime policies, connection strings, etc) and you can use your custom configuration where needed.

    I cannot answer as to why it works in VS but not outside it.  I suspect it is just a quirk with how VS is starting the app.  I personally wouldn't rely on it.  It just seems like you're asking for trouble.  IMHO.

    Michael Taylor - 3/20/2013
    http://msmvps.com/blogs/p3net

    • Marked as answer by Grandblack Friday, March 22, 2013 10:33 AM
    Wednesday, March 20, 2013 5:03 PM
    Moderator
  • Ok thansk. I'll have a look at how to use Configuration which is still unclear to me.

    Friday, March 22, 2013 10:33 AM