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

本文关键字:文件夹 列表 datagridview 显示文件 | 更新日期: 2023-09-27 18:29:15

我有一个应用程序,它显示目录中的文件列表,每当其中有文件超过特定文件限制时,就会弹出一条消息。但不知何故,我无法显示该目录中的子文件夹。我该怎么做。这是我的代码:

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;
        }
     }

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

您可以执行以下重载:

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

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

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