在线人数统计?
  有没有人有更好的办法,能够准确点统计!基于。NET的

解决方案 »

  1.   

    更好的?我提供个常用的吧在Global.asax文件下,
    void Session_Start(object sender, EventArgs e) 
        {
            // 在新会话启动时运行的代码
        }    void Session_End(object sender, EventArgs e) 
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。
        }
    这个Global.asax文件默认没有的,需要手动添加一下才可以
      

  2.   

    首先建立一个文本文件counter.txt,打开文件输入一个大于0的整数作为访问记录的初始值。面我们就可以正式的编写计数器的程序了。 listing 1是webform1.aspx,主要是用于显示从文件中读出的访问次数的记录。由于在整个应用程序生命周期中,Application 对象都是有效的,所以在不同的页面中都可以对它进行存取,就像使用全局变量一样方便。在代码中,使用<%=Application["counter"]%>来表示访问次数记录。程序代码如下: listing1 -----webform1.aspx----- <%@ Page language="c#" Src="WebForm1.aspx.cs" Inherits="counter1.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> </HEAD> <body> <form id="Form1" method="post" runat="server"> <FONT face="宋体">您是第<%=Application["counter"]%>位访问者!</FONT> </form> </body> </HTML> Listing 2和listing3是global.asax和global.asax.cs文件代码,当执行webform1.aspx文件之前会执行它们。在global.asax.cs文件中,定义了一些事件和其响应代码,主要是用于读写文件和数值累加。Listing 2 -----global.asax---- <%@ Application Src="Global.asax.cs" Inherits="counter2.Global" %> listing 3 -----global.asax.cs----- using System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.SessionState; using System.IO ; namespace counter2 { public class Global : System.Web.HttpApplication { protected void Application_Start(Object sender, EventArgs e) { 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 (); //数值累加,注意这里使用了装箱(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) { } protected void Application_EndRequest(Object sender, EventArgs e) { } protected void Session_End(Object sender, EventArgs e) { } 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 (); } } }