App.Config file

S

Stephen

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.
 
K

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.NameValueFileSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="stage" type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section
name="prod" type="System.Configuration.NameValueFileSectionHandler,
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.GetConfig("dev");

-- then return the value you want:

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

-Hope this helps
 
S

Stephen

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
 
K

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.
 
S

Stephen

Hi KJ,

I did search for the machine.config file it has the following:
<section name="appSettings"
type="System.Configuration.NameValueFileSectionHandler, 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
 
K

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.
 
S

Stephen

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
 
S

Stephen

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.NameValueFileSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section name="stage"
type="System.Configuration.NameValueFileSectionHandler,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
<section name="prod"
type="System.Configuration.NameValueFileSectionHandler,
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
 
K

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.
 
K

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top