Results 1 to 3 of 3

Thread: [RESOLVED] How do I get a value for a key using the KeyValueConfigurationElement?

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Resolved [RESOLVED] How do I get a value for a key using the KeyValueConfigurationElement?

    Here's my code to get the three parameters from a configuration!

    C# Code:
    1. Configuration configuration = ConfigurationManager.OpenExeConfiguration(dllConfigFile);
    2.                 var appSettings = configuration.AppSettings.Settings;
    3.                 foreach (KeyValueConfigurationElement keyValueConfigurationElement in appSettings)
    4.                 {
    5.                     switch(keyValueConfigurationElement.Key)
    6.                         {
    7.                         case "WSUser": UserId = keyValueConfigurationElement.Value;
    8.                             break;
    9.                         case "WSUserPassword": UserPassword = keyValueConfigurationElement.Value;
    10.                             break;
    11.                         case "endpointurl": EndPointUrl = keyValueConfigurationElement.Value;
    12.                             break;
    13.                            
    14.                         }
    15.                 }

    I want to eliminate the loop completely and just go after the specific key. I know the key names, so what is the syntax to do this?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How do I get a value for a key using the KeyValueConfigurationElement?

    Your appSettings variable should be a dictionary of some sort, so you can simply index it with the appropriate key to get the appropriate value.

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: How do I get a value for a key using the KeyValueConfigurationElement?

    Found it.

    UserId = appSettings["WSUser"].Value;
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width