统计网站总访问量(c#)

来源:互联网 发布:墨迹天气数据来源 编辑:程序博客网 时间:2024/05/29 04:00

 void Application_Start(Object sender,EventArgs e)

{

int count=0;

System.IO.StreamReader srd;

//获取文件的实际路径

string file_path=Server.MapPath("counter.txt");

srd=System.IO.FileText(file_path);

while(srd.Peek()!=-1)

{

string str=srd.ReadLIne();

count=int.Parse(str);

}

srd.Close();

object obj=count;

Application["counter"]=obj;

}

void Session_Stat(Object sender,EventArgs e)

{

Application.Lock();

int Stat=0;

Stat=(int)Application["counter"];

Stat+=1;

object obj=Stat;

Application["counter"]=obj;

//将数据写入文件

string file_path=Server.MapPath("count.txt");

System.IO.StreamWriter srw=new System.IO.StreamWriter(file_path);

srw.WriteLine(Stat);

srw.Close();

Application.UnLock();

}

void Application_End(object sender,EventArgs)

{

int Stat=0;

Stat=(int)Application["counter"];

System.IO.StreamWriter srw=new System.IO.StreamWriter();

srw.WriteLine(Stat);

srw.Close();

}

原创粉丝点击