6

I have a key in the web.config file like:

<add key="MailFrom" value="[email protected]"/>

I need to access it in the code behind.How to do this in c#?

1 Answer 1

9

You could use the AppSettings property indexer:

string from = ConfigurationManager.AppSettings["MailFrom"];
Sign up to request clarification or add additional context in comments.

2 Comments

Most projects, don't seem to have a reference to System.Configuration.dll by default, which is required to access System.Configuration.ConfigurationManager, even though parts of the System.Configuration namespace are available without it. Worth mentioning for anyone that comes across this question but for a non-WebForms project (as WebForms seems to reference it for you).
@Rob, good remark. The link I provided to the documentation states exactly in which assembly this method is located so if the reader takes his time to check it out there won't be any problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.