是否可以搜索键的所有值并返回键? [英] is it possible to search through all values of keys and return the key?
本文介绍了是否可以搜索键的所有值并返回键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的web.config:
here's my web.config:
<add key="SiteTitle" value="Breaking News, World News, and Multimedia"/>
<add key="SiteName" value="New York Times"/>
<add key="somekey" value="somevalue"/>
是否可以搜索所有值以找到世界新闻"
并返回"SiteTitle"
?
is it possible to search through all values to find "world news"
and return "SiteTitle"
?
我正在使用asp.net c#
i am using asp.net c#
非常感谢您的帮助
推荐答案
我确定可以使用RegEx完成此操作,但是您只需在应用设置中搜索每个KVP的内容即可:(根据您的要求进行了编辑评论)
I'm sure this can be done with a RegEx, but you can just search the contents of each KVP in the app settings: (Edited in response to your comment)
public string GetKeyThatHasMyValue (string Section, string ValueToFind)
{
string Result = null;
var Settings = (NameValueCollection)ConfigurationManager.GetSection (Section);
foreach (string Key in Settings.Keys)
{
if (Settings[Key].Contains (ValueToFind))
{
Result = Key;
break;
}
}
return Result;
}
这也假设您正在使用应用程序设置-否则,请使用Eclyps19的代码来获取您感兴趣的部分,然后使用我的代码进行浏览.
This is also assuming you are using app settings--otherwise use Eclyps19's code to grab the section in which you are interested and then use mine to look through them.
这篇关于是否可以搜索键的所有值并返回键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文