本文介绍了如何检查appsettings中的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app.config文件:



app.config file:

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





我想检查keyList中配置文件中是否存在值123。 />


以下是我的c#代码:





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

推荐答案

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"))
           {

           }


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);

               }
           }


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







搜索所有按键,找到任何一个按键价值123






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");
            }





然后,如果您确定appsettings中的所有密钥都是CSV。您可以执行以下操作来查找包含123(而不是123456)的任何键。





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);    
                    }
                }
            }


这篇关于如何检查appsettings中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:55