0

I hope I'm just missing something simple. I need to read/write to a section of my exe.config file. I have this in my code:

var appConfiguration = ConfigurationManager.OpenExeConfiguration("Mytest.Console.exe");
var fileEnvironment = appConfiguration.GetSection("fileEnvironment");

and this is my app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="fileEnvironment" type="System.Configuration.DictionarySectionHandler"/>
    </configSections>

    <fileEnvironment>
        <add key="TestEntry1" value="A nice value"/>
        <add key="TestEntry2" value="Another value"/>
    </fileEnvironment>
</configuration>

My appConfiguration variable is returned as {System.Configuration.Configuration} and the "HasFile" property is set to true.

Without casting my variable "fileEnvironment" is returned as System.Configuration.DefaultSection. When I add as IDictionary<string, string> to the GetSection method fileEnvironment is null.

Any ideas?

Julian
  • 20,008
  • 17
  • 77
  • 108
JimBoone
  • 554
  • 1
  • 7
  • 27
  • GetSection returns a ConfigurationSection, not a Dictionary, so nothing strange about that. What are you expecting to be able to do with the fileEnvironment variable? – Joachim Isaksson Jan 21 '12 at 12:31
  • Have you check out this previous quesntion and answer... http://stackoverflow.com/questions/1278730/net-configurationmanager-app-config-confusion – Lloyd Jan 21 '12 at 12:31
  • Yes, I read through that one. That's where I learned about DictionarySectionHandler. Anyway, I want to extract the entire fileEnvironment section as a dictionary, modify some parts of it, and save the entire dictionary back to the configuration file. Is it possible to do this? – JimBoone Jan 21 '12 at 12:39
  • Why not create your own custom configuartion setting this is probably a better method to use? http://msdn.microsoft.com/en-us/library/ie/2tw134k3.aspx – Lloyd Jan 21 '12 at 13:02
  • How do I pass the entire dictionary around to the rest of my application. I was hoping to add new entries to the dictionary and insert the new entries into the config file. Is this possible to do? – JimBoone Jan 21 '12 at 13:11
  • I kept researching the dictionary issue and came up with [this stackoverflow topic](http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection)! It produces a collection instead of a dictionary, but points the way to a solution. Thanks for everyone's time. – JimBoone Jan 21 '12 at 15:19

2 Answers2

0

According to this old article, when a section is implemented using DictionarySectionHandler, ConfigurationManager.GetSection() will return a non-generic IDictionary, and not an IDictionary<T,V>. That's why your cast failed.

Although in modern times, it looks like it actually returns a HashTable.

Kurt Hutchinson
  • 2,959
  • 23
  • 30
0

I kept researching the dictionary issue and came up with this stackoverflow Q&A! It produces a collection instead of a dictionary, but points the way to a solution. Thanks for everyone's time.

Community
  • 1
  • 1
JimBoone
  • 554
  • 1
  • 7
  • 27