473,378 Members | 1,066 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,378 software developers and data experts.

App.Config file

Hi,

Is there a way to have conditional statements in app.config file?
I want to add keys, so that based on the args[], appropriate keys are taken
from the config file and used in my app

Thanks,
Stephen.
Jan 17 '06 #1
13 4426
KJ
I can't answer for ASP.NET 2.0. But for ASP.NET 1.1, you can define
custom sections in the config file, then, pass in the custom section
name to your method, and pluck the value from the appropriate section.

So, your web.config might look like this (definition of the custom
sections, followed by custom sections):

<configuration>
<configSections>
<section
name="dev" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>

Then, write a class that retrives the values by section, using a call
such as:

NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig("dev");

-- then return the value you want:

string AppRootDir = nvc["AppRootDir"]; //returns "/Authoring/"

-Hope this helps

Jan 17 '06 #2
Yes it does,
Thanks a lot
Stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I can't answer for ASP.NET 2.0. But for ASP.NET 1.1, you can define
custom sections in the config file, then, pass in the custom section
name to your method, and pluck the value from the appropriate section.

So, your web.config might look like this (definition of the custom
sections, followed by custom sections):

<configuration>
<configSections>
<section
name="dev" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>

Then, write a class that retrives the values by section, using a call
such as:

NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig("dev");

-- then return the value you want:

string AppRootDir = nvc["AppRootDir"]; //returns "/Authoring/"

-Hope this helps

Jan 17 '06 #3
Hi KJ,

I am getting an Error while reading the app.config file at
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

what am i doing wrong?
I am using 1.1.4322 version but where can i find the publickeytoken?

Thanks again,
Stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I can't answer for ASP.NET 2.0. But for ASP.NET 1.1, you can define
custom sections in the config file, then, pass in the custom section
name to your method, and pluck the value from the appropriate section.

So, your web.config might look like this (definition of the custom
sections, followed by custom sections):

<configuration>
<configSections>
<section
name="dev" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>

Then, write a class that retrives the values by section, using a call
such as:

NameValueCollection nvc =
(NameValueCollection)ConfigurationSettings.GetConf ig("dev");

-- then return the value you want:

string AppRootDir = nvc["AppRootDir"]; //returns "/Authoring/"

-Hope this helps

Jan 17 '06 #4
KJ
I think it's in the machine.config file for your version of asp.net (in
C:\WINDOWS\Microsoft.NET\Framework\vXXX\CONFIG). Let me know if you
can't find a working one. I remember having the same problem at first.

Jan 18 '06 #5
Hi KJ,

I did search for the machine.config file it has the following:
<section name="appSettings"
type="System.Configuration.NameValueFileSectionHan dler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

and I am using app.config to feed the Windows application and have done as
suggested and i get the following Error:

An unhandled exception of type 'System.Configuration.ConfigurationException'
occurred in system.dll
Additional information: Could not create
System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a
Stephen

"KJ" <n_**********@mail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I think it's in the machine.config file for your version of asp.net (in
C:\WINDOWS\Microsoft.NET\Framework\vXXX\CONFIG). Let me know if you
can't find a working one. I remember having the same problem at first.

Jan 18 '06 #6
KJ
Try the following values for version:

Version=1.0.3300.0

If this does not work. Open your "microsoft .Net 1.1 Framework
configuration" from the administrative tools. Look in the assembly
cache for the system assembly and check that version number.

Also, another post I read indicated to set the "copy local" flag on the
properites for the system assembly reference to true, but I'm not sure
about that.

Sorry for not having the immediate answer to this.

Jan 18 '06 #7
Hi KJ,

The System assembly is present and it has version: 1.0.5000.0 and Public
Key: b77a5c561934e089
Now i also observed that System.Configuration.NameValueFileSectionHandler is
not registered
How can i register it?

Thanks,
stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Try the following values for version:

Version=1.0.3300.0

If this does not work. Open your "microsoft .Net 1.1 Framework
configuration" from the administrative tools. Look in the assembly
cache for the system assembly and check that version number.

Also, another post I read indicated to set the "copy local" flag on the
properites for the system assembly reference to true, but I'm not sure
about that.

Sorry for not having the immediate answer to this.

Jan 18 '06 #8
KJ
Is it possible to post the relevant sections of your web.config so I
can take a look?

Jan 18 '06 #9
I am creating a Windows application

This is my app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dev"
type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section name="stage"
type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section name="prod"
type="System.Configuration.NameValueFileSectionHan dler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>

<dev>
<add key="AppRootDir" value="/Authoring/"/>
</dev>

<stage>
<add key="AppRootDir" value="/S-Authoring/"/>
</stage>

<prod>
<add key="AppRootDir" value="/P-Authoring/"/>
</prod>
</configuration>
(used the same str as you suggested)

using this in my Windows app (for displaying)
Imports System.Configuration
Imports System.Collections.Specialized

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
nvc = CType(ConfigurationSettings.GetConfig("dev"),
NameValueCollection)
ListBox1.Items.Add("Test Path: " & nvc("AppRootDir"))

End Sub

Thanks,
stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Is it possible to post the relevant sections of your web.config so I
can take a look?

Jan 18 '06 #10
KJ
OK, I was able to replicate your error. I resolved it by opening the
References folder for the project, then click on the System reference,
then hit F4 (properties window), then change the Copy Local setting to
True. Rebuild and run. Let me know.

Jan 18 '06 #11
Hi KJ,

it worked,
but why should you have a local copy?

Thanks,
Stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
OK, I was able to replicate your error. I resolved it by opening the
References folder for the project, then click on the System reference,
then hit F4 (properties window), then change the Copy Local setting to
True. Rebuild and run. Let me know.

Jan 18 '06 #12
KJ
I don't know. However, I did figure out how to get around doing it:

Create another class file in your windows app and have it inherit from
System.Configuration.NameValueFileSectionHandler, for example
(CollectionHandler.cs):

using System;

namespace WindowsApplication1
{
public class CollectionHandler :
System.Configuration.NameValueFileSectionHandler
{

}
}

Then, in your app.config, replace the <configSections> with this:

<configSections>
<section name="dev" type="WindowsApplication1.CollectionHandler,
WindowsApplication1"/>
<section name="stage" type="WindowsApplication1.CollectionHandler,
WindowsApplication1"/>
<section name="prod" type="WindowsApplication1.CollectionHandler,
WindowsApplication1"/>
</configSections>

Guess what? It works, with copy local = False on System.dll.

I couldn't say for sure why though.

Jan 18 '06 #13
Thanks again,
will try the second method too...
stephen

"KJ" <n_**********@mail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I don't know. However, I did figure out how to get around doing it:

Create another class file in your windows app and have it inherit from
System.Configuration.NameValueFileSectionHandler, for example
(CollectionHandler.cs):

using System;

namespace WindowsApplication1
{
public class CollectionHandler :
System.Configuration.NameValueFileSectionHandler
{

}
}

Then, in your app.config, replace the <configSections> with this:

<configSections>
<section name="dev" type="WindowsApplication1.CollectionHandler,
WindowsApplication1"/>
<section name="stage" type="WindowsApplication1.CollectionHandler,
WindowsApplication1"/>
<section name="prod" type="WindowsApplication1.CollectionHandler,
WindowsApplication1"/>
</configSections>

Guess what? It works, with copy local = False on System.dll.

I couldn't say for sure why though.

Jan 18 '06 #14

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

Similar topics

13
by: Maxim Khesin | last post by:
I want to have a config file with my python proggie, satisfying the following requirements: 1) support key->(value, default) 2) simple and intuitive to read and edit 3) easyly readable into a...
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*...
2
by: Suresh Gladstone | last post by:
Hi, This is a bit with versioning and installation of the .NET dlls. I want to perform the following, 1. A third party application will be invoking my .NET dll through COM interop . For this I...
4
by: grs | last post by:
Can a class library have a app.config file. Reason for asking is that the microsoft application blocks all read from myApp.exe.config. How can you use the application blocks if you do not have an...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
6
by: Rich | last post by:
Hello, I picked up this example on using the Reflection namespace for loading forms/classes on the fly at msdn http://msdn.microsoft.com/library/default.asp?...
13
by: Khodr | last post by:
Hello, I am using VS.NET 2003 and vb. I build my application MyApp and it generates MyApp.exe.config. So now MyApp.exe reads parameters from MyApp.exe.config. Great and no problem! I need to...
3
by: Blasting Cap | last post by:
I am working on a web app that I want to be able to use a separate config file on, in addition to the web.config file that's already working in the application. If I put the following in the...
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...
5
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.