473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to read config file?

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/is...s/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("DebugC leanFile",
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

Jun 9 '06 #1
3 15943
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.Log4NetConfigurationSectionHa ndler, 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.AppSett ings["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 Romero wrote:
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/is...s/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("DebugC leanFile",
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


Jun 9 '06 #2
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

Jun 9 '06 #3
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
Brett Romero wrote:
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


Jun 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
3
by: Mustaq | last post by:
Hi, How to read same config file from different application? I have 4 application in VB.NET, all are using different app.config files. NOW I need all to read only one config file, how can I do...
4
by: UJ | last post by:
Is there a way to write into the .config file? In other words treat it like a .ini file. (I may be confused on what the exact nature of the .config file may be.) I can read from it fine but...
5
by: Dica | last post by:
i've followed the instructions i found here about how to add a config file to my applicaiton: http://www.c-sharpcorner.com/Code/2003/Nov/CreateUseConfigFiles.asp the instructions state that i...
10
by: NuB | last post by:
I'm creating a C# class file(DLL) that will be used by an asp.net application. In the DLL I want to read a web.config, or app.config file so some information can change without having to go into...
16
by: Timm | last post by:
I'm trying to use ASP objects (basically formed and populated based on Web.Config settings) and I want to use them in a different non-asp program with minimal reprogramming. So, my question is how...
5
by: Sridhar | last post by:
Hi, I have created a project which contains classes to read the data from the database. This project has an App.Config file which contains the SqlConnection String. when this code is called from...
1
by: M Irfan | last post by:
Hello all, Recently I have started working in .NET 2.0. I am developing a web service application that references a DLL component written in C# 2.0. The component has its own config file which...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.