获得指定目录下的 文件和子目录

FileInfo fi;
   DirectoryInfo di;
   TableCell td;
   TableRow  tr;

   /*
    设定Table中的数据
    首先搞定第一行
   */
   tr = new TableRow();

   td = new TableCell();
   td.Controls.Add(new LiteralControl("<img src='name.gif'>"));
   tr.Cells.Add(td);

   td = new TableCell();
   td.Controls.Add(new LiteralControl("<img src='size.gif'>"));
   tr.Cells.Add(td);

   td = new TableCell();
   td.Controls.Add(new LiteralControl("<img src='lastmodify.gif'>"));
   tr.Cells.Add(td);

   tbDirInfo.Rows.Add(tr);

   string FileName;   //文件名称
   string FileExt;    //文件扩展名
   string FilePic;    //文件图片
   long FileSize;    //文件大小
   DateTime FileModify;  //文件更新时间

   DirectoryInfo dir = new DirectoryInfo(strCurrentDir);
   foreach(FileSystemInfo fsi in dir.GetFileSystemInfos())
   {
    FilePic = "";
    FileName = "";
    FileExt = "";
    FileSize = 0;

    if(fsi is FileInfo)
    {
     //表示当前fsi是文件
     fi = (FileInfo)fsi;
     FileName = fi.Name;
     FileExt  = fi.Extension;
     FileSize = fi.Length;
     FileModify = fi.LastWriteTime;
     //通过扩展名来选择文件显示图标
     switch(FileExt)
     {
      case ".gif":
       FilePic = "gif.gif";
       break;
      default:
       FilePic = "other.gif";
       break;
     }
     FilePic = "<img src='"+FilePic+"' width=25 height=20>";
    }
    else
    {
     //当前为目录
     di = (DirectoryInfo)fsi;
     FileName = di.Name;
     FileModify = di.LastWriteTime;
     FilePic = "<img src='directory.gif' width=25 height=20>";
    }

    //组建新的行
    tr = new TableRow();

    td = new TableCell();
    td.Controls.Add(new LiteralControl(FilePic+"&nbsp;"+FileName));
    tr.Cells.Add(td);

    td = new TableCell();
    td.Controls.Add(new LiteralControl(FileSize.ToString()));
    tr.Cells.Add(td);

    td = new TableCell();
    td.Controls.Add(new LiteralControl(FileModify.ToString()));
    tr.Cells.Add(td);

    tbDirInfo.Rows.Add(tr);
   }

posted @ 2006-10-23 15:07  RobotTech  阅读(459)  评论(0编辑  收藏  举报