How do I read a specific value from a config file?

  • Thread starter Thread starter John Galt
  • Start date Start date
J

John Galt

I am trying to read the "ConnectionString" value from my app.exe.config
file, but am not sure of the format to do this.

It is of the form:

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

<appParams>
<add key="ConnectionString" value="User
Id=MyID;Password=MyPW;Initial Catalog=MyDB;Server=d-sql;" />
</appParams>

etc.

</configuration>

In particular, I am interesting in getting the "Server=d-sql" info.

It seems like is should be something like
System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"),
but that doesn't seem to work.
 
The problem is that this is being used by Microsoft's Data Application
Block. I need the connection screen info to display which system the
application is pointing to (Development, Production, etc.)
 
Are you really sure the tag isn't appSettings ?... For appParams isn't part
of standard .net config files:
If you want to use appParams, you've got to declare it inthe xml and then
the way you access it isn't as simple as for appSettings:

1 - sample App.exe.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="appParams"
type="System.Configuration.NameValueSectionHandler,system,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089,
Custom=null"/>
</configSections>
<appSettings>
<add key="key1" value="value1" />
</appSettings>
<appParams>
<add key="key2" value="value2" />
</appParams>
</configuration>

You'll notice that because appParams isn't standard, you have to declare it
in the configSections part.

2 - Accessing values (in C#) :

using System.Configuration;
using System.Collections.Specialized;

// It's easy to retreive value1:
string s1 = ConfigurationSettings.AppSettings["key1"]; // s1 contains
"value1".

// A little more tricky for value2:
NameValueCollection nvc = ConfigurationSettings.GetConfig("appParams") as
NameValueCollection;
string s2 = nvc["key2"]; // s2 contains "value2".

Olivier DALET
---------------
 
Olivier DALET said:
Are you really sure the tag isn't appSettings ?... For appParams isn't part
of standard .net config files:
If you want to use appParams, you've got to declare it inthe xml and then
the way you access it isn't as simple as for appSettings:

Yes. This is the format used by the Application Block.

Not sure why it works, but it does.
 
What application block are you talking about: I've just downloaded versions
1 and 2 of MS Data Application Block, and I've seen nothing concerning
configuration reading, neither appParams sections...

I've downloaded these blocks at
http://www.microsoft.com/downloads/...FamilyID=76FE2B16-3271-42C2-B138-2891102590AD
(V1)
and
http://www.microsoft.com/downloads/...0A-9877-4A7B-88EC-0426B48DF275&displaylang=en
(V2)

could you tell which block you are using and provide a link to it?

Olivier DALET
---------------
 

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

Back
Top