使用Application对象简单完成网站总访问人数的统计
2024-09-28 16:10:42
Global.asax文件:
using System.IO;
protected void Application_Start(object sender, EventArgs e)
{
FileStream fs = File.Open(Server.MapPath("counter.txt"),FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
Application["count"] = Convert.ToInt32(sr.ReadLine());
sr.Close();
fs.Close();
}
//counter.txt会自动创建
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["count"] = (int)Application["count"] + ;
FileStream fs = File.Open(Server.MapPath("counter.txt"), FileMode.OpenOrCreate,FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(Application["count"]);
sw.Close();
fs.Close();
Application.UnLock();
}
此种方法是把总人数记录在一个记事本文件中,使用文件流读写的方式来更新人数值,较为简单。
最新文章
- Single Number II
- ReactNative之style使用指南
- salt-master 的配置文件详解
- Eclipse编辑java文件报Unhandled event loop exception错误的解决办法
- [Oracle] Group By 语句的扩展 - Rollup、Cube和Grouping Sets
- 用java api读取HDFS文件
- ORACLE解锁数据库用户
- 使用telnet操作memcache,一般不常用
- jQuery插件实现select下拉框左右选择_交换内容(multiselect2side)
- HTML DOM nodeName nodeValue
- Python的下载和安装
- JavaScript 数组最大值
- 前端工程师:电信专业转前端是如何拿到阿里、腾讯offer的?
- 一键安装gitlab7在rehl6.4上
- python下的异常处理
- BZOJ.3252.攻略(贪心 长链剖分/线段树)
- JavaWeb基础-Jsp基础语法
- C# Task注意事项
- SimpleXML概述
- Nginx 限制php解析、限制浏览器访问