Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retrieve a value from App.config

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
Here's my App.config file:
Code:
<?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!

Code:
        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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top