在datagridview中显示文件和子文件夹列表?

时间:2012-03-29 02:10:40

标签: c# winforms

我有一个应用程序,它显示目录中的文件列表,并在其中有超过特定文件限制的文件时弹出消息。但不知怎的,我不能让它显示该目录中的子文件夹。我怎样才能做到这一点。这是我的代码:

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 个答案:

答案 0 :(得分:1)

您可以进行以下重载:

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

这将查找目录中的所有文件(包括子目录),这些文件与第二个参数中传递的模式匹配。

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