Uba
Uba

Reputation: 669

How to read the Key from appsettings?

Is it possible to get the key based on app settings value?

for Example;

string value = "abcd";
string key = ConfigurationManager.AppSettings[value].ToString();

Now, I want the CustomerCategory in key variable.

Upvotes: 0

Views: 992

Answers (2)

Chathuranga Ranasinghe
Chathuranga Ranasinghe

Reputation: 547

If I'm not misunderstood your question, Here's the way to do it.

app.config file should be like this,

 <appSettings>
    <add key="myKey" value="myValue"/>
 </appSettings>

And your C# code

 string myValue = ConfigurationManager.AppSettings["myKey"];

Upvotes: 0

abatishchev
abatishchev

Reputation: 100358

using System.Linq;

string key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(k => 
                 ConfigurationManager.AppSettings[k] == value);

Upvotes: 2

Related Questions