Asp tutorial. net application static variable usage
File: Global. asax
<% @ Application Language = "C #" ClassName = "Global" %>
<% @ Import Namespace = "System. IO" %>
<% @ Import Namespace = "System. Collections. Generic" %>
<Script runat = "server">
Private static string [] fileList;
Public static string [] FileList
{
Get
{
If (fileList = null)
{
FileList = Directory. GetFiles (HttpContext. Current. Request. PhysicalApplicationPath );
}
Return fileList;
}
}
Private static Dictionary <string, string> metadata = new Dictionary <string, string> ();
Public void AddMetadata (string key, string value)
{
Lock (metadata)
{
Metadata [key] = value;
}
}
Public string GetMetadata (string key)
{
Lock (metadata)
{
Return metadata [key];
}
}
</Script>
File: Default. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "StaticApplicationVariables" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.1 // EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Untitled Page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Label ID = "lblInfo" runat = "server"> </asp: Label>
</Div>
</Form>
</Body>
</Html>
File: Default. aspx. cs
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Text;
Using ASP;
Public partial class StaticApplicationVariables: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder ();
Foreach (string file in Global. FileList)
{
Builder. Append (file + "<br/> ");
}
LblInfo. Text = builder. ToString ();
}
}