站点访问历史人数统计的问题
GLOBAL.ASAX中代码如下,现在的问题是,不重启www服务累加没什么问题,但当我停止www服务后,再重新启动www服务,再打开统计页面,结果页面还是显示为1,感觉 Application_End事件中的代码没有执行,请大家帮忙修改一下.
protected void Application_Start(Object sender, EventArgs e)
{
StreamReader objstreamreader;
StreamWriter objstreamwriter;
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath("count.txt")))
{
objstreamreader=File.OpenText(System.Web.HttpContext.Current.Server.MapPath("count.txt"));
Application["totali"]=objstreamreader.ReadLine();
objstreamreader.Close();
}
else
{
Application["totali"]=0;
objstreamwriter=File.CreateText(System.Web.HttpContext.Current.Server.MapPath("count.txt"));
objstreamwriter.WriteLine(Application["totali"].ToString());
objstreamwriter.Close();
}
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["totali"]=Convert.ToInt32(Application["totali"]) + 1;
Application.UnLock();
}
protected void Application_End(Object sender, EventArgs e)
{
//FileStream countwrite=new FileStream(System.Web.HttpContext.Current.Server.MapPath("count.txt"),FileMode.Open);
using(StreamWriter objstream=new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("count.txt").ToString()))
{
objstream.WriteLine(Application["totali"]);
objstream.Close();
}
}
问题点数:50、回复次数:8Top
1 楼copine()回复于 2005-08-04 20:22:30 得分 0
你先看看那个文件建立没有吧,如果你的站点是在interpub里面的话有可能是因为运行asp.net环境的用户没权限写该文件夹造成的。Top
2 楼copine()回复于 2005-08-04 20:23:40 得分 0
还有记得结贴,你的信誉分都76 了。Top
3 楼oracleunix()回复于 2005-08-04 20:33:05 得分 0
该文件的确建立了,另外这个程序在d盘里,做为虚拟目录了,并且该盘为fat32格式Top
4 楼zhilunchen(他山居士)回复于 2005-08-04 20:50:37 得分 0
用数据库吧!Top
5 楼oracleunix()回复于 2005-08-05 06:33:34 得分 0
有人能帮忙解决一下吗?急Top
6 楼zeusvenus()回复于 2005-08-05 07:44:53 得分 0
试试
通过在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
}
}
Top
7 楼bingbingcha(不思不归,不孟不E,原来是头大灰狼)回复于 2005-08-05 07:52:57 得分 0
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath("count.txt")))
这个路径必须是你网站目录内..如果是其他盘就写全路径("D:")
如果依然问题存在,就输入这个目录内的文件看一下..Top
8 楼LoveMango(幻风)回复于 2006-03-13 13:14:48 得分 0
markTop




