当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

C#中获取文件列表的方法

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:47:11

:2011-03-18 09:27:03

下面GetFileList方法负责列出指定目录的文件列表。

  • /// <summary>   
  • /// 获取文件列表   
  • /// </summary>   
  • private void GetFileList()   
  • {   
  •     string strCurDir,FileName,FileExt;   
  •        
  •     /**////文件大小   
  •     long FileSize;   
  •        
  •     /**////最后修改时间;   
  •     DateTime FileModify;   
  •   
  •     /**////初始化   
  •     if(!IsPostBack)   
  •     {   
  •         /**////初始化时,默认为当前页面所在的目录   
  •         strCurDir = Server.MapPath(".");   
  •         lblCurDir.Text = strCurDir;   
  •         txtCurDir.Text = strCurDir;   
  •     }   
  •     else  
  •     {   
  •         strCurDir = txtCurDir.Text;   
  •         txtCurDir.Text = strCurDir;   
  •         lblCurDir.Text = strCurDir;   
  •     }   
  •     FileInfo fi;   
  •     DirectoryInfo dir;   
  •     TableCell td;   
  •     TableRow tr;   
  •     tr = new TableRow();   
  •        
  •     /**////动态添加单元格内容   
  •     td = new TableCell();   
  •     td.Controls.Add(new LiteralControl("文件名"));   
  •     tr.Cells.Add(td);   
  •     td = new TableCell();   
  •     td.Controls.Add(new LiteralControl("文件类型"));   
  •     tr.Cells.Add(td);   
  •     td = new TableCell();   
  •     td.Controls.Add(new LiteralControl("文件大小"));   
  •     tr.Cells.Add(td);   
  •     td = new TableCell();   
  •     td.Controls.Add(new LiteralControl("最后修改时间"));   
  •     tr.Cells.Add(td);   
  •   
  •     tableDirInfo.Rows.Add(tr);   
  •        
  •     /**////针对当前目录建立目录引用对象   
  •     DirectoryInfo dirInfo = new DirectoryInfo(txtCurDir.Text);   
  •        
  •     /**////循环判断当前目录下的文件和目录   
  •     foreach(FileSystemInfo fsi in dirInfo.GetFileSystemInfos())   
  •     {   
  •         FileName = "";   
  •         FileExt = "";   
  •         FileSize = 0;   
  •            
  •         /**////如果是文件   
  •         if(fsi is FileInfo)   
  •         {   
  •             fi = (FileInfo)fsi;   
  •                
  •             /**////取得文件名   
  •             FileName = fi.Name;   
  •                
  •             /**////取得文件的扩展名   
  •             FileExt = fi.Extension;   
  •                
  •             /**////取得文件的大小   
  •             FileSize = fi.Length;   
  •                
  •             /**////取得文件的最后修改时间   
  •             FileModify = fi.LastWriteTime;   
  •         }   
  •   
  •         /**////否则是目录   
  •         else  
  •         {   
  •             dir = (DirectoryInfo)fsi;   
  •                
  •             /**////取得目录名   
  •             FileName = dir.Name;   
  •                
  •             /**////取得目录的最后修改时间   
  •             FileModify = dir.LastWriteTime;   
  •                
  •             /**////设置文件的扩展名为"文件夹"  
  •             FileExt = "文件夹";   
  •         }   
  •            
  •         /**////动态添加表格内容   
  •         tr = new TableRow();   
  •         td = new TableCell();   
  •         td.Controls.Add(new LiteralControl(FileName));   
  •         tr.Cells.Add(td);   
  •         td = new TableCell();   
  •         td.Controls.Add(new LiteralControl(FileExt));   
  •         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("yyyy-mm-dd hh:mm:ss")));   
  •         tr.Cells.Add(td);   
  •         tableDirInfo.Rows.Add(tr);   
  •     }   

如果你能读懂这段程序,那么你只要进行适当的修改就可以完成你需要的操作


C#中获取文件列表的方法