.aspx文件 注(本例需要建立一个文本文件)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace test
{
public partial class counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
if (Session["OnLine"].ToString() == "true")
{
Application.Lock();
count = (int)Application["counter"];
count = count + 1;
object obj = count;
Application["counter"] = obj;
//将数据记录写入文件
string strFilePath = Server.MapPath("counter.txt");
StreamWriter SW = new StreamWriter(strFilePath, false);
SW.WriteLine(count);
SW.Close();
Session["count"] = count.ToString();
Application.UnLock();
Session["OnLine"] = "false";
}
else
{
count = Convert.ToInt32(Session["count"]);
}
string strCounter = count.ToString();
string imgName;
Response.Write("您是第");
for (int i = 0; i < strCounter.Length; i++)
{
imgName = strCounter.Substring(i, 1);
Response.Write(" <img alt='lib' src='image/" + imgName + ".gif' style='width: 39px; height: 39px' />");
}
Response.Write("位访问者");
}
}
}
.asax文件
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.IO;
namespace test
{
public class Global : System.Web.HttpApplication
{
private System.ComponentModel.IContainer componetns = null;
protected void Application_Start(object sender, EventArgs e)
{
int count = 0;
StreamReader stream;
string FilePth = Server.MapPath("counter.txt");
stream = File.OpenText(FilePth);
while (stream.Peek() != -1)
{
string str = stream.ReadLine();
count = Int32.Parse(str);
}
object obj = count;
Application["counter"] = obj;
stream.Close();
}
protected void Session_Start(object sender, EventArgs e)
{
Session["OnLine"] = "true";
Session["count"] = 0;
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
int count = 0;
count = (int)Application["counter"];
//将记录写入文件
string FilePath = Server.MapPath("counter.txt");
StreamWriter SW =new StreamWriter(FilePath, false);
SW.WriteLine(count);
SW.Close();
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace test
{
public partial class counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
if (Session["OnLine"].ToString() == "true")
{
Application.Lock();
count = (int)Application["counter"];
count = count + 1;
object obj = count;
Application["counter"] = obj;
//将数据记录写入文件
string strFilePath = Server.MapPath("counter.txt");
StreamWriter SW = new StreamWriter(strFilePath, false);
SW.WriteLine(count);
SW.Close();
Session["count"] = count.ToString();
Application.UnLock();
Session["OnLine"] = "false";
}
else
{
count = Convert.ToInt32(Session["count"]);
}
string strCounter = count.ToString();
string imgName;
Response.Write("您是第");
for (int i = 0; i < strCounter.Length; i++)
{
imgName = strCounter.Substring(i, 1);
Response.Write(" <img alt='lib' src='image/" + imgName + ".gif' style='width: 39px; height: 39px' />");
}
Response.Write("位访问者");
}
}
}
.asax文件
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.IO;
namespace test
{
public class Global : System.Web.HttpApplication
{
private System.ComponentModel.IContainer componetns = null;
protected void Application_Start(object sender, EventArgs e)
{
int count = 0;
StreamReader stream;
string FilePth = Server.MapPath("counter.txt");
stream = File.OpenText(FilePth);
while (stream.Peek() != -1)
{
string str = stream.ReadLine();
count = Int32.Parse(str);
}
object obj = count;
Application["counter"] = obj;
stream.Close();
}
protected void Session_Start(object sender, EventArgs e)
{
Session["OnLine"] = "true";
Session["count"] = 0;
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
int count = 0;
count = (int)Application["counter"];
//将记录写入文件
string FilePath = Server.MapPath("counter.txt");
StreamWriter SW =new StreamWriter(FilePath, false);
SW.WriteLine(count);
SW.Close();
}
}
}