views:

77

answers:

2

Hello,

I'm working with C#, Framework 3.5 (VS 2008).

I'm using the ConfigurationManager to load a config (not the default app.config file) into a Configuration object.

using the Configuration class, i was able to get an ConfigurationSection, but i could not find a way to get the values of that section.

in the config, the ConfigurationSection is of type "System.Configuration.NameValueSectionHandler".

For what it worth, when i used the method "GetSection" of the ConfigurationManager (works only when it was on my default app.config file), i received an object type, that i could cast into collection of pairs of key-value, and i just received the value like a Dictionary. i could not do such cast when i received ConfigurationSection class from the Configuration class however.

Thanks alot, Liran.

EDIT:

Example of the config file:

<configuration>
  <configSections>
    <section name="MyParams" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <MyParams>
    <add key="FirstParam" value="One"/>
    <add key="SecondParam" value="Two"/>
  </MyParams>
</configuration>

Example of the way i was able to use it when it was on app.config (the "GetSection" method is for the default app.config only):

NameValueCollection myParamsCollection =
             (NameValueCollection)ConfigurationManager.GetSection("MyParams");

Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);
+1  A: 

Here's a good post that shows how to do it.

http://bloggingabout.net/blogs/jschreuder/archive/2006/07/07/Using-section-handlers-to-group-settings-in-the-configuration-file_2E00_.aspx

If you want to read the values from a file other than the app.config, you need to load it into the ConfigurationManager.

Try this method: ConfigurationManager.OpenMappedExeConfiguration()

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration%28v=VS.90%29.aspx

There's an example of how to use it in the MSDN article.

MonkeyWrench
As i said in my post, This is what i did. I received Configuraion class, and from it i received a ConfigurationSection.My question was how can i get the values from that ConfigurationSection object? i didn't ask how to get the Configuration object..Thanks anyway!
Liran B
by the way, in the example you gave me they are not using the Configuration class that comes from OpenMappedExeConfiguration, but they are using the default app.config (which can be used using the .GetSection/.GetConfig methods, but as i said in my post- i did that already. but its no good for me since its good for app.config only).and for the msdn, i could not find the answer to my question there..
Liran B
Ok, the more I look into it, the more I see it isn't easy to do. The basic problem is that you're trying to mix .Net 1.1 configuration style code and 2.0. Read this, it'll point you in the right direction: http://www.eggheadcafe.com/software/aspnet/30832856/backwards-compatibility-of-systemconfigurationconfiguration.aspx
MonkeyWrench
A: 

Thank you MonkeyWrench,

so i undestand that there is a no "nice" way (nice = the word "XML" wont come up in my code) to read a group of key-value pairs from a config other then the app.config, without creating my own custom ConfigurationSection.. that just too bad.

Thanks again for your time, Liran.

Liran B