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"] + 1; 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(); }
此种方法是把总人数记录在一个记事本文件中,使用文件流读写的方式来更新人数值,较为简单。