如何检查appsettings中的键值 [英] How to check key values in appsettings

查看:100
本文介绍了如何检查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

推荐答案

试试这个



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

           }


还可以查看以下链接。



ConfigurationManager类(System.Configuration) [ ^ ]



ConfigurationManager.AppSettings属性(System.Configuration) [ ^ ]



Please请尝试以下。



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

               }
           }


所以我提供了2种方法来找出你想要的东西。查看appsettings中的任何键,无论键名如何,然后通过keyname查找键的具体值。



我的appsettings:



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>







搜索所有按键,找到任何一个按键价值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中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆