C#网页计数器

来源:互联网 发布:凭淘宝账号贷款 编辑:程序博客网 时间:2024/04/30 20:47

longcounter=1;//声明一个计数器变量,并赋初值为1;

private void Page_Load(objectsender,System.EventArgse)
{
 //在此处放置用户代码以初始化页面
 Application.Lock(); //进行锁定,防止非同步更新
 string countfile=Server.MapPath("count.txt");
 if(File.Exists(countfile))
 {
  StreamReader sReader = new StreamReader(countfile);
  string s;
  long x;
  s = sReader.ReadLine();
  if(s!=null)
  {
   x=Convert.ToInt64(s);
   counter=x+1;
  }
  sReader.Close();
 } //endif
 StreamWriter sWriter = new StreamWriter(countfile);
 sWriter.Write((counter.ToString())); //将long类型转换为string类型
 sWriter.Flush();
 sWriter.Close();
 Application.UnLock();

 count.Text="您是本站第"+counter+"位访客";

}  

  注意,通过这个方法生成的count.txt文件只有在创建者,即第一个登陆该站的人拥有写入权限,其他访问者在向count.txt文件中计数时将报错,无法向count.txt执行写入操作,需要将count.txt文件的安全属性中的相应用户的写入权限打开。

原创粉丝点击