.NET 2.0 ConfigurationManager


J

Janiek Buysrogge

Hello,

Can somebody please tell me how I can read values from the following
configuration file. It is generated by VS 2K5 Express when I add
settings using the project properties front-end:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="applicationsettings.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<applicationsettings.Properties.Settings>
<setting name="Test" serializeAs="String">
<value>this is a test value</value>
</setting>
</applicationsettings.Properties.Settings>
</applicationSettings>
</configuration>

The code I have:

Configuration config =
ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
ConfigurationSection appSettings =
config.GetSection("applicationsettings.Properties.Settings");
ConfigurationSectionGroup appSettingsGroup =
config.GetSectionGroup("applicationSettings");
ConfigurationSectionCollection appSettingsSections =
appSettingsGroup.Sections;
foreach (ConfigurationSection section in appSettingsSections)
{
Console.WriteLine(section.SectionInformation.Name);

// stuck here, how do I get children ?
}

First of all, the code I've written seems much to complicated, just
for reading out a simple value.

Second: then what is the correct way to do it, I've read MSDN docs but
they are all based on the AppSettings section. I would like to keep
the xml intact, and read out the value of "Test" in the
"applicationSettings.Properties.Settings" section.

Since MS are saying all other methods of accessing the app.config are
rendered obsolete by the ConfigurationManager class, there must be
some way to do it.

Any help would be greatly appreciated,

Janiek Buysrogge
 
Ad

Advertisements

A

Aboulfazl Hadi

Hi
I think you can use the following code snippet to access settings that
is generated by Project Designer (Project Properties )'s Setting Pane:

Properties.Settings.Default.yourSettingName

Best Regards,
A.Hadi
 
J

Janiek Buysrogge

Hi,

Thank you for your quick answer.

You got me a step closer, I can see the name of my variable in the
intellisense list.

However, I get this error when executing the code:

A first chance exception of type
'System.Configuration.ConfigurationErrorsException' occurred in
System.Configuration.dll
Configuration system failed to initialize

This also occurs when I put the OpenExeConfiguration before the
Properties call.

Is there any extra initialization required ?

Thanks in advance,

Janiek
 
K

Kevin Spencer

Hi Janiek,

The biggest difference between .Net 1.1 and .Net 2.0 in terms of
Configuration is that in .Net 2.0 everything is strongly typed. For each
Configuration Section there is a specific type of class that handles it.
Your best bet is to create your own custom ConfigurationSection and
ConfigurationElement derived classes to handle your own custom Configuration
Sections.

Here's a good starting point to learn how:

http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top