这是通过在Global.asax文件中配置Application来统计的方法......


using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.IO ;

namespace movie
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.IContainer components = null;

   public Global()
   {
    InitializeComponent();
   }
 
   protected void Application_Start(Object sender, EventArgs e)
   {
             Application["conn"]="Server=localhost;database=movie;uid=sa;pwd='zcc';";
    Application["user_sessions"] = 0;
    Application["counter_num"]=0;


    uint count=0;
    StreamReader srd;
    //取得文件的实际路径
    string file_path=Server.MapPath ("counter.txt");
    //打开文件进行读取
    srd=File.OpenText (file_path);
    while(srd.Peek ()!=-1)
    {
     string str=srd.ReadLine ();
     count=UInt32.Parse (str);
    }
    object obj=count;
    Application["counter"]=obj;
    srd.Close ();

   }

   protected void Session_Start(Object sender, EventArgs e)
   {
    Application.Lock();
    Application["user_sessions"] = (int)Application["user_sessions"] + 1;
    Application.UnLock();

    Application.Lock ();
    //数值累加,注意这里使用了装箱(boxing)
    uint jishu=0;
    jishu=(uint)Application["counter"];
    jishu=jishu+1;
    object obj=jishu;
    Application["counter"]=obj;
    //将数据记录写入文件
    string file_path=Server.MapPath ("counter.txt");
    StreamWriter fs=new StreamWriter(file_path,false);
    fs.WriteLine (jishu);
    fs.Close ();
    Application.UnLock ();


   }

   protected void Application_BeginRequest(Object sender, EventArgs e)
   {

//    Application.Lock();
//    Application["counter_num"]=(int)Application["counter_num"]+1;
//    Application.UnLock();

   }

   protected void Application_EndRequest(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)
   {
    Application.Lock();
    Application["user_sessions"] = (int)Application["user_sessions"] - 1;
    Application.UnLock();

   }

   protected void Application_End(Object sender, EventArgs e)
   {

    uint js=0;
    js=(uint)Application["counter"];
    //object obj=js;
    //Application["counter"]=js;
    //将数据记录写入文件
    string file_path=Server.MapPath ("counter.txt");
    StreamWriter fs=new StreamWriter(file_path,false);
    fs.WriteLine(js);
    fs.Close ();


   }
  
   #region Web 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {   
    this.components = new System.ComponentModel.Container();
   }
   #endregion
}
}

posted on 2009-12-22 09:05  kenryuu  阅读(368)  评论(0编辑  收藏  举报