[英]AppSettings find key by value
I have a hopefully a very simple question. 我希望这是一个非常简单的问题。
I want to find a certain key from AppSettings in Web.config if i have the value 我想在Web.config中找到AppSettings中的某个键,如果我有值的话
So to keep it simple is there a possibility to find AppSettings entry by specifying the value instead of the key 因此,为了保持简单,可以通过指定值而不是键来查找AppSettings条目
<appSettings>
<add key="my:Hello" value="world"/>
<add key="my:Test" value="New"/>
<add key="my:Test2" value="SecondThing"/>
etc...
</appSettings>
lets say i have somewhere inside my code world
and i want to find the appsetting's key Hello
. 让我说我的代码
world
里面有一个地方,我想找到appsetting的关键Hello
。 Is there a possibility to do so? 有没有可能这样做?
I do not want to get questions on why i want to do this or answers like just save this inside some DB. 我不想问我为什么要这样做或者只是将其保存在某个数据库中的答案。
you could do something like this: 你可以这样做:
var matches = ConfigurationManager.AppSettings.AllKeys.Select(t =>
new { Key = t, Value =
ConfigurationManager.AppSettings[t] }).Where(i => i.Value == "world");
(Obviously this returns a key/value pair, you could add .Select(k => k.Key); to just get the key. Also note, using where will return multiples potentially, you could use SingleOrDefault/FirstOrDefault as an alternative) (显然这会返回一个键/值对,你可以添加.Select(k => k.Key);来获取密钥。另请注意,使用where返回可能的倍数,你可以使用SingleOrDefault / FirstOrDefault作为替代)
You could retrieve all keys and find the one that contains the value: 您可以检索所有密钥并找到包含该值的密钥:
foreach (string key in ConfigurationManager.AppSettings)
{
if (ConfigurationManager.AppSettings[key] == "world")
{
return key;
}
}
var settings = ConfigurationManager.AppSettings;
var keys = settings.AllKeys.Where(k => settings[k] == "world");
but note - values are not unique. 但请注意 - 值不是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.