Asp.net 计数器
来源:互联网 发布:java线程join方法 编辑:程序博客网 时间:2024/05/20 19:16
在工程下面添加一个Global.asax 文件,如果是在vs2005 工程下添加的话 会出现没有.cs 文件,这个时候得手动修改,方法如下:
1、在App_Code 里面添加一个类,起名为 Global.cs
2、把public class Global 改为 public partial class Global : System.Web.HttpApplication
3、把下面方法粘贴到里面,添加 IO 命名空间引用
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
//计数器
string path = Server.MapPath("") + "\\" + "counter.txt";
if (!File.Exists(path))
{
StreamWriter sw = File.CreateText(path);
sw.WriteLine("0");
sw.Close();
}
StreamReader sr = File.OpenText(path);
string str = sr.ReadToEnd();
long count = Int32.Parse(str);
Application["count"] = count;
sr.Close();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
//计数器
Application.Lock();
Application["count"] = (long)Application["count"] + 1;
long count = (long)Application["count"];
string path = Server.MapPath("") + "\\" + "counter.txt";
StreamWriter sw = new StreamWriter(path, false);
sw.WriteLine(count);
sw.Close();
}
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}
4、添加 Global.asax 文件 指定下 Inherits ,Global 就是刚才添加的那个Global.cs 文件,如下:
<%@ Application Language="C#" Inherits="Global" %>
这样就完成Global.asax 文件的添加了。
5、页面显示计数器: 浏览次数:<%=Convert.ToInt32((Application["count"])).ToString()%>
如果是在vs2008 上用计数器的话就把 Application_Start 和 Session_Start 方法里面的代码复制到 Global.asax 相对应的方法里面就Ok了。
- Asp.net 计数器
- ASP.NET性能计数器
- ASP.NET 的性能计数器
- asp.net 的性能计数器
- asp.net 的性能计数器
- asp.net网站图片计数器!
- 用ASP.NET制作计数器
- asp.net 中使用计数器
- 用ASP.NET制作计数器
- ASP.NET 的性能计数器
- 文本型单页面ASP.NET计数器
- 简单的Asp.net计数器控件
- ASP.NET:XML计数器第二版
- ASP.NET:XML计数器第二版
- 文本型单页面ASP.NET计数器
- ASP计数器
- 【转载】文本型单页面ASP.NET计数器
- asp.net中用C#实现站点计数器用户控件
- Eclipse中文注释乱码解决
- Spring--quartz中cronExpression
- Struts2异常:ognl.OgnlException: target is null for setProperty
- MPI实现fft的迭代算法 源于并行计算——结构。算法。编程中伪码 更新1
- MPI实现fft的迭代算法 源于并行计算——结构。算法。编程中伪码 更新2
- Asp.net 计数器
- 关于dword ptr 指令
- 黑马程序员-- .Net学习日记——c#中的循环结构
- 设计模式的五大设计原则
- ffmpeg解码流程
- linux 查看依赖库
- java 使用sourceforge.pinyin4j查询汉字拼音
- 黑马程序员-- .Net学习日记——c#面向对象-方法
- Oracle数据库system用户忘记了密码怎么办?