2012-03-29 132 views
0

我有一个应用程序,它显示目录中的文件列表,并会弹出一条消息,只要有一个文件超过了一定的文件限制。但不知何故,我不能让它显示该目录内的子文件夹。我怎样才能做到这一点。这里是我的代码:在datagridview中显示文件和子文件夹的列表?

public partial class Form1 : Form 
{ private Timer timer; 
    private int count; 
    DataTable dt = new DataTable(); 
    DataRow dr; 
    String[] s1; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     count = 0; 
     timer = new Timer(); 
     timer.Interval = 1000; 
     timer.Tick += new EventHandler(timer1_Tick); 
     timer.Start(); 
     //Initialize Directory path 
     s1 = Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\FILE"); 
     //File Name, File Type, File size, create date 
     for (int i = 0; i <= s1.Length - 1; i++) 
     { 
      if (i == 0) 
      { 
       //Add Data Grid Columns with name 
       dt.Columns.Add("File_Name"); 
       dt.Columns.Add("File_Type"); 
       dt.Columns.Add("File_Size"); 
       dt.Columns.Add("Create_Date"); 
      } 
      //Get each file information 
      FileInfo f = new FileInfo(s1[i]); 
      FileSystemInfo f1 = new FileInfo(s1[i]); 
      dr = dt.NewRow(); 
      //Get File name of each file name 
      dr["File_Name"] = f1.Name; 
      //Get File Type/Extension of each file 
      dr["File_Type"] = f1.Extension; 
      //Get File Size of each file in KB format 
      dr["File_Size"] = (f.Length/1024).ToString(); 
      //Get file Create Date and Time 
      dr["Create_Date"] = f1.CreationTime.Date.ToString("dd/MM/yyyy"); 
      //Insert collected file details in Datatable 
      dt.Rows.Add(dr); 


      if ((f.Length/1024) > 5000) 
      { 
       MessageBox.Show("" + f1.Name + " had reach its size limit."); 
      } 
      else 
      { } 

     } 
     if (dt.Rows.Count > 0) 
     { 
      //Finally Add DataTable into DataGridView 
      dataGridView1.DataSource = dt; 
     } 
    } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      count++; 
      if (count == 60) 
      { 
       count = 0; 
       timer.Stop(); 
       Application.Restart(); 
      } 
     } 
     public string secondsToTime(int seconds) 
     { 
      int minutes = 0; 
      int hours = 0; 

      while (seconds >= 60) 
      { 
       minutes += 1; 
       seconds -= 60; 
      } 
      while (minutes >= 60) 
      { 
       hours += 1; 
       minutes -= 60; 
      } 

      string strHours = hours.ToString(); 
      string strMinutes = minutes.ToString(); 
      string strSeconds = seconds.ToString(); 

      if (strHours.Length < 2) 
       strHours = "0" + strHours; 
      if (strMinutes.Length < 2) 
       strMinutes = "0" + strMinutes; 
      if (strSeconds.Length < 2) 
       strSeconds = "0" + strSeconds; 

      return strHours + ":" + strMinutes + ":" + strSeconds; 
     } 
    } 

回答

1

您可以在以下过载:

Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\FILE", 
        "*", 
        SearchOption.AllDirectories) 

这将查找在目录(包括子目录)的所有文件,匹配第二个参数传递的图案,其。

此外,返回的文件名包括所有文件的完整路径,因此您可以正确处理它们。

+0

谢谢@suddnely_me !! 它完美的作品。但是我可以问一个问题,是否可以在datagridview中显示文件夹,我们可以将其扩展并最小化。因为如果它在一个显示器中显示包括子文件夹中文件在内的所有文件,将会非常混乱。 – 2012-03-29 02:25:39

+0

是的,是的,你可以! (c)这里最好的方法是组合TreeView来显示目录结构和DataGridView来显示当前所选目录的内容。这将是像Windows资源管理器,除非你有一个DataGridView,而不是文件列表。 – iehrlich 2012-03-29 02:29:03

+0

顺便说一句我想我明白你想得到什么,但AFAIK DataGridView不能单独做到这一点。 – iehrlich 2012-03-29 02:30:13

相关问题