How to read config file?


B

Brett Romero

I'd like to use some of the techniques discussed here for reading a
config file in .NET 2.0:
http://msdn.microsoft.com/msdnmag/issues/06/06/ConfigureThis/default.aspx

Here's my app.config content:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="DebugCleanFile" type="System.Boolean,
ConfigurationProperty, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" value="true"/>
</configSections>

<system.diagnostics>
</system.diagnostics>
</configuration>


Here's the code snippet:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
class RunProg
{
public static void Main( string[ ] args )
{
AppConfigFileEngine appconfig = new AppConfigFileEngine();
object debugClean = appconfig.DebugCleanFile.DefaultValue;
}
}

class AppConfigFileEngine :
System.Configuration.ConfigurationSection
{

public System.Configuration.ConfigurationProperty
DebugCleanFile = new
System.Configuration.ConfigurationProperty("DebugCleanFile",
typeof(bool));

}
}

The default value on debugClean is always false. I also get this
warning from the app.config file:

The 'value' attribute is not declared.

I simply want to read a different properties, along with their set
value, out of the config file using something similar to the above
structure. What am I doing wrong?

I'd also like to declare debugClean as a bool instead of object. How
can that be done?

Thanks,
Brett
 
Ad

Advertisements

D

Dinsdale

Two qualifiers to this response: 1) I work in .Net 1.1 and 2) I'm not
all that up on my custom sections

First, it seems to me that you are declaring your value and type
attributes in the Section definition, which from my understanding is
not legal. After you remove those, you must declare the section in the
configSections tag and then declare the actual section itself somewhere
bellow the configSections area.
An example of a config section:

<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

....

Then further in your config file:

<log4net debug="false">
....stuff here
</log4net>

How to read that back out of the config is up to you, I don't usually
use custom sections. I generally only use the pre-defined appSettings
section and use the <add> tag.

<appSettings>
<add key="test" value="testing"/>
</appSettings>

Then I read values out using the following:

string strMyVal =
System.Configuration.ConfigurationSettings.AppSettings["test"];

Anyway, I'm pretty sure about the configSection stuff. You'll need to
revisit that as I noted above.

That article you mentioned looks good. I'll have to dig into that when
I get a chance.

Cheers
Russ

Brett said:
I'd like to use some of the techniques discussed here for reading a
config file in .NET 2.0:
http://msdn.microsoft.com/msdnmag/issues/06/06/ConfigureThis/default.aspx

Here's my app.config content:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="DebugCleanFile" type="System.Boolean,
ConfigurationProperty, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" value="true"/>
</configSections>

<system.diagnostics>
</system.diagnostics>
</configuration>


Here's the code snippet:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
class RunProg
{
public static void Main( string[ ] args )
{
AppConfigFileEngine appconfig = new AppConfigFileEngine();
object debugClean = appconfig.DebugCleanFile.DefaultValue;
}
}

class AppConfigFileEngine :
System.Configuration.ConfigurationSection
{

public System.Configuration.ConfigurationProperty
DebugCleanFile = new
System.Configuration.ConfigurationProperty("DebugCleanFile",
typeof(bool));

}
}

The default value on debugClean is always false. I also get this
warning from the app.config file:

The 'value' attribute is not declared.

I simply want to read a different properties, along with their set
value, out of the config file using something similar to the above
structure. What am I doing wrong?

I'd also like to declare debugClean as a bool instead of object. How
can that be done?

Thanks,
Brett
 
B

Brett Romero

I've tried that:

<configSections>
<section name="DebugCleanFile" type="System.Boolean,
ConfigurationProperty, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>

<DebugCleanFile value="true" />

but it still doesn't pick it up. I basically want to define the type
for the variable, which <appSettings> doesn't let you do. This way I
can bring a typed variable (vs. always a string) into the app without
casting. I want to bring the bool type straight from the app.config
into a class.

Brett
 
Ad

Advertisements

D

Dinsdale

I've started using serialization to store applicaiton configuration in
my simpler apps. I create objects that contain all my config data and
then serialize it to Xml. No casting necessary and I get a greater
level of control over the how my data is organized. Also gives alot
more flexability. That may be overkill for your particular problem
though. I'd be interested to see the solution to getting data without
having to cast.


Russ
 

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