.NET ConfigurationManager app.config confusion

21,801

Solution 1

Here's how I did it:

-Make sure that System and System.Configuration.dll are available in your bin directory. You do this by adding the reference to both dlls and set their copy local attribute to true.

-Make sure in your App.Config file, set the copy local attributes to false.

-Use the following method:

public static string XMLCheck
{
    get
    {
        var section =(Hashtable)ConfigurationManager.GetSection("PowershellSnapIns");
        return (string)section["SnapIn1"];

    }
}

-The XMLCheck should return "WebAdministration"

Solution 2

VS renames the app.config to yourappname.exe and places it in the bin directory along with your .exe

Can you confirm that the file exists in the bin directory along with your exe.

Share:
21,801
scope_creep
Author by

scope_creep

Updated on August 15, 2020

Comments

  • scope_creep
    scope_creep over 3 years

    I've got an app.config file, which contains the following

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name ="PowershellSnapIns" type ="System.Configuration.DictionarySectionHandler,System"/>
        </configSections>
    
        <PowershellSnapIns>
            <add key="SnapIn1" value="WebAdministration" />
            <add key="SnapIn2" value="Jimmy Hendrix" />
            <add key="SnapIn3" value="..." />
        </PowershellSnapIns>
    </configuration>
    

    I was going to use the ConfigurationSettings class to read it, but that has been deprecated. That was fairly simple to use. Now I have to use the ConfigurationManager class, and I now have this code to read it.

     System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
    IDictionary SnapInList = (IDictionary) config.GetSection("PowershellSnapIns");
    

    But it keeps erroring out. I changed the app.config properties to copy across to the build, but it keeps accepting that it can't find the file. The exception says it's looking for TestConsole.vshost.exe.config. Does vs2k8sp1 now rename the app.config for you automatically, and if so, what am I doing wrong? Surely I don't need to rename the app.config file to debug vhost. I do know in release that it's probably being renamed TestConsole.exe.config. So what's going wrong? Is it the case of the code wrong or what?

  • scope_creep
    scope_creep over 14 years
    The key doesn't matter, since I don't need to know the key, just the value, which is is the powershell snapin name.
  • scope_creep
    scope_creep over 14 years
    The file TestConsole.vhost.exe.config is in the Visual Studio 2008\Projects\TestConsole\TestConsole\bin\Debug directory.
  • scope_creep
    scope_creep over 14 years
    Hi Ngu Soon Hui, It worked. Thanks. I can see now (hindsight is beautiful) that the ConfigurationManager class is static. Thanks.
  • Priya Narayanan
    Priya Narayanan almost 12 years
    Works with a cast to (NameValueCollection) for me.