WEB遍历文件夹下所有的文件(不遍历文件夹)

  1.  /// <summary>
  2.     /// 根据文件夹路径得到文件路径下所有的文件
  3.     /// </summary>
  4.     /// <param name="pathtext"></param>
  5.     public void ListFiles(string pathtext)
  6.     {
  7.         try
  8.         {
  9.             
  10.             if (pathtext == null ||pathtext.Equals(""))
  11.             {
  12.                 Response.Write("路径不能为空错误");
  13.             }
  14.             else
  15.             {
  16.                 //得到文件名,目录
  17.                 string[] filename = Directory.GetFiles(Server.MapPath(pathtext));
  18.                 //初始化文件计数器
  19.                 int a = 0;
  20.                 //初始化文件大小
  21.                 long j = 0;
  22.                 //遍历文件夹
  23.                 foreach (string file in filename)
  24.                 {
  25.                     //得到文件的创建时间
  26.                     DateTime CreationTime = File.GetCreationTime(file);//文件创建时间
  27.                     //得到符合条件的文件,某个时间段之前的文件
  28.                     //file文件地址
  29.                     Response.Write(file+"file<br/>");
  30.                     //得到文件的大小
  31.                     FileInfo f = new FileInfo(file);
  32.                     long len = f.Length;//单个文件大小
  33.                     Response.Write(len.ToString()+"文件大小<br/>");
  34.                     j = j + len;
  35.                     a++;//文件个数
  36.                 }
  37.                 //关闭窗口
  38.                 Response.Write (a.ToString()+"文件个数<br/>");//输出个数
  39.                 int i = a;
  40.                 //Response.Write(j.ToString());
  41.                 long x = j /1024;
  42.                 Response.Write(x.ToString() + "KB 文件大小<br/>");//得到大小
  43.                 Response.Write("搜索完毕!系统提示<br/>");
  44.             }
  45.         }
  46.         catch
  47.         {
  48.             Response.Write("请检查路径是否正确");
  49.         }
  50.     }
posted on 2008-11-12 12:54  叮叮猫的编程世界  阅读(382)  评论(0编辑  收藏  举报