标签:
1.把Count.txt和dayCount.txt两个文件放至App_Data目录下
2.把Global.asax.txt中的代码复制至Global.asax文件中即可
3.在需要使用"今日访问量"的地方:Application["dayCounter"]
4.在需要使用"历史访问量"的地方:Application["Counter"]
<%@ Application Language="C#" %>
<script runat="server">
//功能:网站的访问量统计(今日访问量,总访问量)
//作者:丁礼奎
//日期:2012-04-12
public static string strs; //保存从文本中读取的信息
public static string day;//记录文本中访问时间
void Application_Start(object sender, EventArgs e)
{
/**********总访问量****************/
int intCount = 0;
System.IO.StreamReader sr;
string serverFile = Server.MapPath("~/App_Data/Count.txt");
sr = System.IO.File.OpenText(serverFile);
while (sr.Peek() != -1)
{
string strCount = sr.ReadLine();
intCount = int.Parse(strCount);
}
sr.Close();
object obj = intCount;
Application["counter"] = obj;
/*********************************/
/**********今日访问量****************/
Application["dayCounter"] = 0;
Application["day"] = DateTime.Now.ToString();
/*********************************/
}
void Application_End(object sender, EventArgs e)
{
/**********总访问量****************/
int intStat = 0;
intStat=(int)Application["counter"];
string serverFile = Server.MapPath("~/App_Data/Count.txt");
System.IO.StreamWriter sr = new System.IO.StreamWriter(serverFile, false);
sr.WriteLine(intStat);
sr.Close();
/*********************************/
/**********今日访问量****************/
int Stat = 0;
Stat = (int)Application["dayCounter"];
string day0 = (string)Application["day"]; //保存日期
string str = Stat.ToString() + "," + day0.ToString();
string file_path = Server.MapPath("~/App_Data/dayCount.txt"); // 将数据记录写入文件
System.IO.StreamWriter srw = new System.IO.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 intStat = 0;
intStat = (int)Application["counter"];
++intStat;
object objCount = intStat;
Application["counter"] = objCount;
string serverFile = Server.MapPath("~/App_Data/Count.txt");
System.IO.StreamWriter sr = new System.IO.StreamWriter(serverFile, false);
sr.WriteLine(intStat);
sr.Close();
/*********************************/
/**********今日访问量****************/
int count;// 记录文本中的日访问量
string NowDay; //记录文本中访问时间
System.IO.StreamReader srd;
string file_path = Server.MapPath("~/App_Data/dayCount.txt"); //取得文件的实际路径
srd = System.IO.File.OpenText(file_path); //打开文件进行读取
while (srd.Peek() != -1)
{
strs = srd.ReadLine(); //保存从文件中读取的信息
}
srd.Close();
string[] str = strs.Split(‘,‘); //将读取的信息存放在字符串数组str中
count = Convert.ToInt32(str[0]); //日访问量
day = str[1]; //最近一次访问时间
NowDay = DateTime.Now.ToString();
//文件中保存的时间值与系统时间相比,如果系统时间大,则重新开始计数
if (DateTime.Compare(Convert.ToDateTime(NowDay), Convert.ToDateTime(day)) >= 0)
{
count = 0;
day = DateTime.Now.AddDays(1).ToShortDateString() + " " + "00:00:00"; //day保存下一天的开始时间
string NewDayStr = "0" + "," + day.ToString();
// 将数据记录写入文件
//string file_path0 = Server.MapPath("counter.txt");
System.IO.StreamWriter srw0 = new System.IO.StreamWriter(file_path, false);
srw0.WriteLine(NewDayStr);
srw0.Close();
}
object objcount = count;
object objday = day;
//日访问量
Application["dayCounter"] = objcount;
//最近一次访问时间
Application["day"] = objday;
// 数据累加
int Stat = 0;
//获取Application对象中的日访问量
Stat = (int)Application["dayCounter"];
Stat += 1;
object obj = Stat;
Application["dayCounter"] = obj;
//保存日期
string day0 = (string)Application["day"];
string str0 = obj.ToString() + "," + day0.ToString();
// 将数据记录写入文件
//string file_path0 = Server.MapPath("counter.txt");
System.IO.StreamWriter srw1 = new System.IO.StreamWriter(file_path, false);
srw1.WriteLine(str0);
srw1.Close();
/*********************************/
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
//Session["num"]=Convert.ToInt16(Session["num"].ToString()) - 1;
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
</script>
标签:
原文地址:http://www.cnblogs.com/loyung/p/4421809.html