Introduction
I dislike having to type out System.Configuration.ConfigurationManager.AppSettings["Foo"]
every time I need to reference an application setting in the
Web.Config.
You can add System.Configuration
to the using
section to cut it down to Configuration.ConfigurationManager["Foo"]
but that's still too much typing for someone lazy like me. Plus it makes my code look untidy.
Here is a simple extension method to make that job easier (or at least cut down on the typing)
Using the code
Simply place this extension method into a static class in your web application....
(You'll also need to add System.Configuration
to the using
section)
public static string AppSetting(this string Key)
{
string ret = string.Empty;
if (System.Configuration.ConfigurationManager.AppSettings[Key] != null)
ret = ConfigurationManager.AppSettings[Key];
return ret;
}
and when the time comes to get the value of an application setting key (say to load a label named "Foo") all you need to do is this:
Foo.Text = "SettingName".AppSetting();
Very simple, just saves time and typing.
Developing software since 1995, starting out with Borland Turbo C++ 3 & Motorolla Assembly.
Eventually learning several different languages such as
Pascal/Delphi,Basic, Visual Basic,FoxPro, ASP, PHP, and Perl.
Currently using the .net framework for most development task.
Hobbies include underwater robotics, and electronics.
George's current language of choice is C#.