一個顯示訪客數的例子

.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();
        }
    }
}
發佈了104 篇原創文章 · 獲贊 5 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章