473,624 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" ?>
<configuratio n>

<configSections >
<section name="DebugClea nFile" type="System.Bo olean,
ConfigurationPr operty, Version=1.0.0.0 , Culture=neutral ,
PublicKeyToken= null" allowDefinition ="Everywhere "
allowExeDefinit ion="MachineToA pplication"
restartOnExtern alChanges="true " value="true"/>
</configSections>

<system.diagnos tics>
</system.diagnost ics>
</configuration>
Here's the code snippet:

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace TestConsole
{
class RunProg
{
public static void Main( string[ ] args )
{
AppConfigFileEn gine appconfig = new AppConfigFileEn gine();
object debugClean = appconfig.Debug CleanFile.Defau ltValue;
}
}

class AppConfigFileEn gine :
System.Configur ation.Configura tionSection
{

public System.Configur ation.Configura tionProperty
DebugCleanFile = new
System.Configur ation.Configura tionProperty("D ebugCleanFile",
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 15960
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.C onfig.Log4NetCo nfigurationSect ionHandler, 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.Configur ation.Configura tionSettings.Ap pSettings["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" ?>
<configuratio n>

<configSections >
<section name="DebugClea nFile" type="System.Bo olean,
ConfigurationPr operty, Version=1.0.0.0 , Culture=neutral ,
PublicKeyToken= null" allowDefinition ="Everywhere "
allowExeDefinit ion="MachineToA pplication"
restartOnExtern alChanges="true " value="true"/>
</configSections>

<system.diagnos tics>
</system.diagnost ics>
</configuration>
Here's the code snippet:

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace TestConsole
{
class RunProg
{
public static void Main( string[ ] args )
{
AppConfigFileEn gine appconfig = new AppConfigFileEn gine();
object debugClean = appconfig.Debug CleanFile.Defau ltValue;
}
}

class AppConfigFileEn gine :
System.Configur ation.Configura tionSection
{

public System.Configur ation.Configura tionProperty
DebugCleanFile = new
System.Configur ation.Configura tionProperty("D ebugCleanFile",
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="DebugClea nFile" type="System.Bo olean,
ConfigurationPr operty, Version=1.0.0.0 , Culture=neutral ,
PublicKeyToken= null" allowDefinition ="Everywhere "
allowExeDefinit ion="MachineToA pplication"
restartOnExtern alChanges="true " />
</configSections>

<DebugCleanFi le 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="DebugClea nFile" type="System.Bo olean,
ConfigurationPr operty, Version=1.0.0.0 , Culture=neutral ,
PublicKeyToken= null" allowDefinition ="Everywhere "
allowExeDefinit ion="MachineToA pplication"
restartOnExtern alChanges="true " />
</configSections>

<DebugCleanFi le 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
3821
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* access config files. This is version 3, which is a big overhaul. It extends ConfigObj to reading config files with sections and various other simplifications. I find ConfigObj extremely easy to use and use it for reading config files and data...
3
2598
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 this. Thanks in Adv. Regards,
4
1567
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 would also like to be able to write to it. TIA - Jeff.
5
2334
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 create a config file named after the executable, so, as my exe is timeTracker.exe, i've created a file called timeTracker.exe.config and placed it in the bin/debug folder. i then added a reference to the file in VS by selecting "File/Add existing...
10
2539
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 the code itself. How can I have the dll read a app.config or web.config file for data? The information I'm looking to store in the file is servernames, file locations, etc. thanks
16
1983
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 do I mock up what ASP does to read the Web.Config file? I've tried messing around in the System.Configuration library and haven't been able to find anything from taking a quick look at the objects and their methods (for the goal of populating the...
5
2282
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 a web application and if I need to read the connection string it is reading the connection string from the web.config of web application. I am not knowing how to change the Application domain so that it reads the App.config file instead of...
1
7482
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 contains settings related to business logic coded in that component. I can reference and load the component but unable to read load the custom config file of that component. I know that the same thing is possible if my host application is any Exe...
12
13423
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 file was built by draging tables from the Data Sources pane. Auto-generated code created the files associated wtih the XSD file (xss,
0
8174
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.