遍历文件下所有文件

来源:互联网 发布:淘宝升到一钻 编辑:程序博客网 时间:2024/05/09 17:12
 
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         if(!IsPostBack){
  4.         string paths = @"F:/学习专用文件夹/网页";
  5.         Boolean flag = ReadFiles(paths);
  6.         if (flag)
  7.         {
  8.             Txtmess.Text = Directory.GetFiles(paths).ToString();
  9.         }
  10.         else
  11.         {
  12.             Response.Write("对不起,有误");
  13.         }
  14.     }
  15.     }
  16.     private Boolean ReadFiles(string dirroot)
  17.     {
  18.         Boolean flag = false;
  19.         string[] rootdirs = Directory.GetDirectories(dirroot);//当前目录的子目录
  20.         string[] rootFiles = Directory.GetFiles(dirroot);//目录下的文件
  21.         try
  22.         {
  23.             foreach (string s2 in rootFiles)
  24.             {
  25.                 Response.Write(s2+"<br/>");
  26.             }
  27.             foreach (string s1 in rootdirs)
  28.             {
  29.                 ReadFiles(s1);
  30.             }
  31.             flag = true;
  32.         }
  33.         catch (Exception ex)
  34.         {
  35.             throw new Exception(ex.Message);
  36.         }
  37.         return flag;
  38.     }
原创粉丝点击