Get a value from App.config

G

Guest

Here's my App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="customers">
<section name="test1"
type="System.Configuration.DictionarySectionHandler" />
<section name="test2"
type="System.Configuration.DictionarySectionHandler" />
</sectionGroup>
</configSections>

<appSettings>
<add key="ConnectionString" value="some value here" />
</appSettings>

<customers>
<test1>
<add key="CustCode" value="0001" />
<add key="FtpHost" value="ftp://xxx.com" />
<add key="FtpAccount" value="user1" />
<add key="FtpPassword" value="password1" />
<add key="Active" value="Y" />
</test1>
<test2>
<add key="CustCode" value="0002" />
<add key="FtpHost" value="ftp://yyy.com" />
<add key="FtpAccount" value="user2" />
<add key="FtpPassword" value="password2" />
<add key="Active" value="Y" />
</test2>
</customers>
</configuration>

I'm trying to loop through customers group and retrieve the 'Active' key
value for each section (test1 and test2). The only way of doing this that I
found is use the Hashtable (see code below). The problem is that those key
values don't come in the order in which they are defined in the config file,
and I have to loop through all the keys until I find 'Active'. How can I
directly get the value I want? Thanks for any help!

Dim config As Configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim custGroup As ConfigurationSectionGroup =
config.SectionGroups.Get("customers")

Dim custCodes As String = Nothing
For Each custNode As DefaultSection In custGroup.Sections
Dim cfg As Hashtable =
CType(ConfigurationManager.GetSection(custNode.SectionInformation.SectionName), Hashtable)

Dim CustCode As String = Nothing
Dim FtpHost As String = Nothing
Dim FtpAccount As String = Nothing
Dim FtpPassword As String = Nothing
Dim Active As String = Nothing
For Each custAtt As System.Collections.DictionaryEntry In cfg
Select Case custAtt.Key
Case "CustCode"
CustCode = custAtt.Value
Case "FtpHost"
FtpHost = custAtt.Value
Case "FtpAccount"
FtpAccount = custAtt.Value
Case "FtpPassword"
FtpPassword = custAtt.Value
Case "Active"
Active = custAtt.Value
End Select
Next
 
M

Mr. Arnold

Again, you should go download the MS Application Block Settings examples for
..Net Framework 2.0 and learn from the examples.

Why try to re-invent the wheel when you can pillage and plunder existing
examples and come-up with a viable solution? :)
 

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

Top