网站在线人数统计(貌似不能写入文件,有错请指正,谢谢!)
来源:互联网 发布:数据库唯一约束 编辑:程序博客网 时间:2024/04/28 03:24
using System;
using System.IO;
namespace Application_Count
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["strPath"]=Server.MapPath("~/CountFile/visitCount.txt");
//Application["strPath"]=Server.MapPath(null)+@"/CountFile/visitCount.txt";
//对比格式,两种都可以
string strPath = Application["strPath"].ToString();//将路径保存在变量中
if( System.IO.File.Exists(strPath))//若文件存在
{
//读取文件
StreamReader sr=new StreamReader (strPath);
string strCount=sr.ReadToEnd();//从头读到尾
if(strCount!=null)
{
Application["Count"]=Convert.ToInt32(strCount);//保存状态为当前值
}
else
{
Application["Count"]=0;
}
}
else //文件不存在时
{
//创建一个文件
{
StreamWriter sw=File.CreateText(strPath);//创建一个文本文件并写入指定路径
Application["Count"]=0;
sw.Write( Application["Count"].ToString());//将计数量存在文件文本中
sw.Close();
}
}
}
protected void Session_Start(object sender, EventArgs e)
{
//string strPath = Application["strPath"].ToString();
//StringWriter sw = new StringWriter();
//string类型不能在构造函数中带参,就没有了写入的路径,所以选择StringBuilder
System.Text.StringBuilder strPath = new System.Text.StringBuilder(Application["strPath"].ToString());
Application.Lock();
Application["Count"] = Convert.ToInt32(Application["Count"]) + 1;
StringWriter sw = new StringWriter(strPath);
sw.Write(Application["Count"].ToString());//将新的访问量存在文件中
sw.Close();
Application.UnLock();
}
protected void Application_BeginRequest(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)
{
System.Text.StringBuilder strPath = new System.Text.StringBuilder(Application["strPath"].ToString());
//如果要修改字符串而不创建新对象,可以用StringBuilder类(在同一处内存修改)
Application.Lock();
Application["Count"] = Convert.ToInt32(Application["Count"]) - 1;
StringWriter sw = new StringWriter(strPath);
sw.Write(Application["Count"].ToString());//将新的访问量存在文件中
sw.Close();
Application.UnLock();
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
注:先创建一个Global文件,再在根目录下添加名为CountFile的文件夹。
- 网站在线人数统计(貌似不能写入文件,有错请指正,谢谢!)
- 统计网站在线人数
- 统计网站 在线人数
- 统计网站在线人数
- 统计网站在线人数
- 统计网站在线人数
- 统计网站的在线人数
- java 统计网站在线人数
- 20171216区间dp(有错请各位大佬指正)
- 统计在线人数和历史人数,写入数据库!
- 注册的在线人数/统计在线人数/网站总访问量
- 用java统计网站在线人数
- 网站的在线人数的统计
- 统计java网站访问量和在线人数
- 简单的网站在线人数统计
- 用java统计网站在线人数
- SSH框架,统计网站在线人数
- java 统计网站访问量&在线人数
- 一篇关于JavaIo的文章
- 树型控件dhtmlxtree使用注意事项~
- 功能强大的树型控件dhtmlxTree
- delphi with语句的说明(开域语句)
- (转)servfox 源码分析
- 网站在线人数统计(貌似不能写入文件,有错请指正,谢谢!)
- ps教程,教你做最简单最美丽的相片
- Socrates Plato 设计速记
- 心得
- 想做个会飞动的椭圆 但是不见效果
- API函数大全
- 函数重载的特点
- 【C2】对话框【S2】Signals and Slots in Depth
- 靠不住