troubleshooting Question

Retrieve a value from App.config

Avatar of EwS
EwS asked on
Visual Basic.NET
3 Comments1 Solution801 ViewsLast Modified:
Here's my App.config file:

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

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

      <add key="CustCode" value="0001" />
      <add key="FtpHost" value="" />
      <add key="FtpAccount" value="user1" />
      <add key="FtpPassword" value="password1" />
      <add key="Active" value="Y" />      
      <add key="CustCode" value="0002" />
      <add key="FtpHost" value="" />
      <add key="FtpAccount" value="user2" />
      <add key="FtpPassword" value="password2" />
      <add key="Active" value="Y" />      

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? And if I know the 'CustCode' value (such as "0001"), how can I retrieve all the other values for 'test1'?

Thanks for any help!

        Dim config As Configuration =
        Dim custGroup As ConfigurationSectionGroup =

 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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros