统计网站访问量的问题
//保存从文本中读取的信息
public static string strs;
//记录文本中访问时间
public static string day;
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Application["counter"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
int Stat = 0;
Stat = (int)Application["counter"];
//保存日期
string str = Stat.ToString();
// 将数据记录写入文件
string file_path = Server.MapPath("counter.txt");
StreamWriter srw = new StreamWriter(file_path, false);
srw.WriteLine(str);
srw.Close();
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
Application.Lock();
int count;// 记录文本中的日访问量
// string NowDay; //记录文本中访问时间
StreamReader srd;
string file_path = Server.MapPath("~/counter.txt"); //取得文件的实际路径
srd = File.OpenText(file_path); //打开文件进行读取
while (srd.Peek() != -1)
{
strs = srd.ReadLine(); //保存从文件中读取的信息
}
srd.Close();
count = Convert.ToInt32(strs); //日访问量
object objcount = count;
//日访问量
Application["counter"] = objcount;
// 数据累加
int Stat = 0;
//获取Application对象中的日访问量
Stat = (int)Application["counter"];
Stat += 1;
object obj = Stat;
Application["counter"] = obj;
//保存日期
string str0 = obj.ToString();
// 将数据记录写入文件
//string file_path0 = Server.MapPath("counter.txt");
StreamWriter srw1 = new StreamWriter(file_path, false);
srw1.WriteLine(str0);
srw1.Close();
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
System.Web.Security.FormsAuthentication.SignOut();
} 这个是代码,有时候刷新页面后访问量会变成0,大家看看有什么问题