You could use a variable globally in your .NET website if you declare it in your Web.config file.
in your Web.config file find <appSettings> tag and insert your variables before it's closing tag:
<appSettings>
<add key=
"MyVariable1" value=
"False" />
<add key=
"MyName" value=
"Damon Serji" />
</appSetting>
Now you can easily access this variable and its value from your C# code:
string myVariable = System.Configuration.ConfigurationSettings.AppSettings["MyName"];
To break the above code down and to make sense of it: System really looks in your Web.Config file and the rest of the line becomes more obvious when you look at your Web.Config structure:
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
.
.
</configSections>
.
<appSettings>
<add key="MyVariable1" value="False" />
<add key="MyName" value="Damon Serji" />
</appSetting>
.
.
So your Web.Config is nothing but a simple XML. System.Configuration tells the compiler to look in to <configuration> tag and System.Configuration.ConfigurationSettings.AppSettings tells it specifically to look into the <appSettings> tag. The result is a simple array of other tags which were added by you manually, so to find a specific tag you need to give it the index of the array i.e. an integer, or the "key" for that array, which is another way to find the tag you are looking for.
Posted by Damon Serji on
22. September 2009 18:06 under:
Intermediate