Reputation: 2766
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
So to keep it simple is there a possibility to find AppSettings entry by specifying the value instead of the key
<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
. 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.
Upvotes: 1
Views: 821
Reputation: 5194
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)
Upvotes: 1
Reputation: 236308
var settings = ConfigurationManager.AppSettings;
var keys = settings.AllKeys.Where(k => settings[k] == "world");
but note - values are not unique.
Upvotes: 1
Reputation: 4808
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;
}
}
Upvotes: 1