2012-03-29 133 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

相關問題