Click here to Skip to main content
16,017,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
app.config file:

<appSettings>
    <add key="KeyList" value="123,234,345,456"/>
  </appSettings>


I want to check if value "123" exists in config file in keyList.

Below is my c# code:

              kk = textBox1.Text;
//kk will be "123"
            keyList = new List<int>();
            if(ConfigurationManager.AppSettings.AllKeys.Contains(kk);
            {
                MessageBox.Show(name);
            }


What I have tried:

I am unable to get it.
can anyone help me
Posted
Updated 19-Sep-16 5:13am
v2

try this

public bool IsContainsValue(string find)
      {
          bool flag = false;
          foreach (string key in ConfigurationManager.AppSettings.AllKeys)
          {
             string _value = ConfigurationManager.AppSettings[key].ToString();
             if (_value.Contains(find))
             { flag = true; break; }
          }
          return flag;

      }


if (IsContainsValue("123"))
           {

           }
 
Share this answer
 
v2
have a look at the below links also .

ConfigurationManager Class (System.Configuration)[^]

ConfigurationManager.AppSettings Property (System.Configuration)[^]

Please try below .

string  option = "123"; // read from text box
          string KeyName = "KeyList"; // key name as in app.config file

           if (ConfigurationManager.AppSettings.AllKeys.Contains(KeyName))
           {
               //key exist
               String KeyValue = ConfigurationManager.AppSettings[KeyName].ToString();
               if (KeyValue.Trim().Contains(option))
               {
                   //Key value found
                   //MessageBox.Show(name);

               }
           }
 
Share this answer
 
v2
Comments
F-ES Sitecore 19-Sep-16 11:06am    
What if Keylist was "1234,5678"? Your code would say that 123 was found, but that's an incorrect result.
Mathew Soji 19-Sep-16 11:38am    
Thank You , that's right . I just wanted to show a sample code .
Should have used split , anyways its suggested in solution 2 .
So I've provided 2 ways of finding out what you want. Looking through any key in appsettings regardless of the key name, and then finding the specific value of the key by keyname.

My appsettings:

<appSettings>
    <add key="KeyList" value="123,234,345,456"/>
    <add key="AnotherSetting" value="123,234,345,456"/>
  </appSettings>



Searching through all keys to find any key with a value of 123

var allkeys = ConfigurationManager.AppSettings.AllKeys;

            foreach (var key in allkeys)
            {
                var value = ConfigurationManager.AppSettings[key];

                if (value.Contains("123"))
                {
                    Console.WriteLine("My app setting has a value of 123 on key {0}", key);
                }
            }


Picking the key by a specified name and checking the value.

var specificKey = ConfigurationManager.AppSettings["KeyList"];

            if (!string.IsNullOrEmpty(specificKey) && specificKey.Contains("123"))
            {
                Console.WriteLine("KeyList contains 123");
            }


Then if you are sure that all keys in your appsettings will be a CSV. You can do something like this to find any key that contains 123 (not 123456).

var csvKey = ConfigurationManager.AppSettings.AllKeys;

            foreach (var key in csvKey)
            {
                var values = ConfigurationManager.AppSettings[key].Split(',');

                foreach (var item in values)
                {
                    if (item == "123")
                    {
                        Console.WriteLine("My app setting has a value of 123 on key {0}", key);    
                    }
                }
            }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900