一个显示访客数的例子

.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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值