Wednesday, February 7, 2007

C# 2.0 Web.Config read AppSettings ConfigurationManager

Problem:
How to read AppSetting in Web.Config file

<appSettings>
<add key="IsMaximized" value="True"/>
<add key="IsUsed" value="False"/>
</appSettings>

How do you get the value for the key = IsUsed?

Answer
There are three ways.

1) string sGet = ConfigurationManager.AppSettings.Get("IsUsed");

2) string sIndexer = ConfigurationManager.AppSettings["IsUsed"].ToString();

3)
using System.Collections.Specialized;

NameValueCollection nvc = ConfigurationManager.AppSettings;
string sNVC = nvc["IsUsed"];

No comments: