Asp.net application static variable usage

Source: Internet
Author: User

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 ();
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.