posts - 77, comments - 54, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

简易的计数器程序

Posted on 2006-06-08 15:48 东人EP 阅读(246) 评论(0)  编辑 收藏 引用 所属分类: .NET
 1 using  System;
 2 using  System.Collections;
 3 using  System.ComponentModel;
 4 using  System.Data;
 5 using  System.Drawing;
 6 using  System.Web;
 7 using  System.Web.SessionState;
 8 using  System.Web.UI;
 9 using  System.Web.UI.WebControls;
10 using  System.Web.UI.HtmlControls;
11 using  System.IO;
12
13 namespace  count
14 {
15      ///   <summary>
16      ///  index 的摘要说明。
17      ///   </summary>

18      public   class  index : System.Web.UI.Page
19      {
20          private   void  Page_Load( object  sender, System.EventArgs e)
21          {
22              //  在此处放置用户代码以初始化页面
23
24              // 计数器初始化为0
25              int  iCount = 0 ;
26              // 存放计数器的文件
27              string  strPath = Server.MapPath( " count.txt " );
28              // 准备打开文件
29             StreamReader ReadFile;
30              try
31              {
32                  // 打开文件
33                 ReadFile = File.OpenText(strPath);
34                  // 读取文件的第一行
35                  string  strCount = ReadFile.ReadLine().Trim();
36                  // 把第一行转化为数字
37                 iCount = int .Parse(strCount);
38                  // 关闭文件
39                 ReadFile.Close();
40             }

41              catch
42              {}
43
44              // 计数器加一
45             iCount ++ ;
46
47              // 输出
48             Response.Write( " <p align>您是第 " + iCount + " 位浏览者</p> " );
49
50              // 准备写文件
51             StreamWriter WriterFile;
52              // 建立一个文件
53             WriterFile = File.CreateText(strPath);
54              // 把计数器写入文件中
55             WriterFile.WriteLine(iCount);
56              // 关闭文件
57             WriterFile.Close();
58         }

59
60          Web Form Designer generated code
79     }

80 }

81
只有注册用户登录后才能发表评论。