none
ConfigurationManager.AppSettings not reading my app.config

    Question

  • I am having a frustrating issue that everyone tells me should be very simple to execute.
    I'm using the new ConfigurationManager object from System.Configuration.dll which I have referenced in my project.  I do not have any compliation problems or warnings, yet I cannot get a single key value out of my app.config file.  My config file looks like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <appSettings>
        <add key="KeyName" value="KeyValue"/>
      </appSettings>
    </configuration>

    and am trying to just assign this to a string like this:

    string path =  System.Configuration.ConfigurationManager.AppSettings["KeyName"];

    not only does this invariably result in an empty string, but also when I step to this line in my code...System.Configuration.ConfigurationManager.AppSettings.AllKeys evaluates to {Dimensions:[0]} as if I don't even have an App.Config file.  What could I possibly be doing wrong!?!?!?!


    Thursday, August 17, 2006 8:43 PM

Answers

  • The root of the problem here is still that the static property "AppSettings" is empty because it is not finding any keys...therefore, in this code example 'appSettings' would also be empty.

    However, I have discovered my problem...being the bone-head that I am, I only included the app.config in my primary project, not the NUnit project I was testing it from.  So effectively my first statemnt was right...it was acting as if there were no app.config...cause there wasn't!!  Hope this helps anyone else who makes this ametuer mistake!
    Friday, August 18, 2006 12:13 AM

All replies

  • I'm not familiar with this 'new' method...the only way I know is:

    string path = ConfigurationSettings.AppSettings["Application Name"];

    Hope I'm not telling you something you already know...
    Is this method VS 2005?

    Steve

    Thursday, August 17, 2006 9:36 PM
  • yeah that's the 1.1 method, it is considered obsolete because it is replaced by ConfigurationManager in 2.0
    Thursday, August 17, 2006 9:47 PM
  • Found this on MSDN:

    // Show how to use AppSettings.
    static void DisplayAppSettings()
    {
    
        // Get the AppSettings collection.
        NameValueCollection appSettings =
           ConfigurationManager.AppSettings;
    
        string[] keys = appSettings.AllKeys;
    
        Console.WriteLine();
        Console.WriteLine("Application appSettings:");
    
        // Loop to get key/value pairs.
        for (int i = 0; i < appSettings.Count; i++)
    
            Console.WriteLine("#{0} Name: {1} Value: {2}",
              i, keysIdea, appSettingsIdea);
    
    }
    Thursday, August 17, 2006 9:54 PM
  • The root of the problem here is still that the static property "AppSettings" is empty because it is not finding any keys...therefore, in this code example 'appSettings' would also be empty.

    However, I have discovered my problem...being the bone-head that I am, I only included the app.config in my primary project, not the NUnit project I was testing it from.  So effectively my first statemnt was right...it was acting as if there were no app.config...cause there wasn't!!  Hope this helps anyone else who makes this ametuer mistake!
    Friday, August 18, 2006 12:13 AM
  • I use a seperate class to control my DB access. I would instantiate and then call the methods from that class with the input parameters and stored procedure I am using. The class is then suppose to make the connection to the DB. Am I wrong to say that the app.config should only be in the DB access class? That's what I'm doing, but it's not picking anything up.
    Monday, July 09, 2007 8:50 AM
  • No, in fact I believe that is exactly incorrect.  After I read the previous post a couple time, I understood what he meant.  If you are using a separate class in a DLL which is referenced by your main project, the "app.config" that is associated with that DLL project has no effect.  The "app.config" must be in the project of the main EXE project.. your client app, or test application, or whatever.

     

    • Proposed as answer by jimbob456456 Monday, October 15, 2012 1:46 PM
    • Unproposed as answer by jimbob456456 Monday, October 15, 2012 1:46 PM
    Wednesday, October 31, 2007 1:15 AM
  • May be this will help you
    string myvalue = ConfigurationManager.AppSettings.Get("KeyName");

    Friday, May 23, 2008 3:28 AM
  • HI, 

    Use

    string my_Con = ConfigurationSettings.AppSettings["connectionstring"],ToString();

    instead of

    string path =  System.Configuration.ConfigurationManager.AppSettings["KeyName"];

    Friday, November 19, 2010 12:59 PM
  • Below Code snippet worked for me also I Had to removed it from the <head> tag of the Page.

        <script language="javascript" src="includes/tradeBreakMoney.js?<%=System.Configuration.ConfigurationManager.AppSettings["StaticFileVersion"]%>" type="text/javascript"></script>


    Tuesday, February 01, 2011 2:02 PM
  • thanks I was just about to stuck my head to the side wall and wolla just saw your post :)
     it works I just had to add the app.config file as a link to my main project.
    Thursday, September 22, 2011 8:25 AM
  • Me salvaste la vida cn tu solucion!!! No me di cuenta que debia agregar el app.config al Unit test. Y nose me hubiese ocurrido nuncaaaa!!!

    Que noobie ¬¬

    Wednesday, November 23, 2011 7:39 PM