需要建立一个根文件夹 ~/xml/couter.txt #region 网站访问量 protected void pageviews() { int count = 0; //数据累加 int Stat = 0; StreamReader srd; //取得文件的实际路径 string file_path = Server.MapPath("/XML/counter.txt"); //打开文件进行读取 srd = File.OpenText(file_path); while (srd.Peek() != -1) { string str = srd.ReadLine(); count = int.Parse(str); } srd.Close(); // 在新会话启动时运行的代码 Application.Lock(); //获取Application对象中保存的网站总访问量 Stat = count; Stat += 1; object obj = Stat; Application["counter"] = obj; //将数据记录写入文件 StreamWriter srw = new StreamWriter(file_path, false); srw.WriteLine(Stat); srw.Close(); Application.UnLock(); } #endregion
时间: 2024-10-26 10:52:49