Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET ConfigurationManager app.config confusion

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?

like image 928
scope_creep Avatar asked Aug 14 '09 16:08

scope_creep


2 Answers

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"

like image 168
Graviton Avatar answered Sep 28 '22 05:09

Graviton


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.

like image 20
jkelley Avatar answered Sep 28 '22 05:09

jkelley