M.Heart
M.Heart

Reputation: 101

Displaying list of files and subfolders in datagridview?

I have an application where it displays list of files within a directory and will pop up a message whenever there is a file in it that exceeds a certain file limit. But somehow I cannot make it to display the sub folders within that directory. How can I do this. Here is my code :

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

Upvotes: 0

Views: 12185

Answers (1)

iehrlich
iehrlich

Reputation: 3580

You can the following overload:

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

this will look for all files in your directory (including subdirectories), which match the pattern passed in the second parameter.

Also, file names returned include full path to all files, so you can process them properly.

Upvotes: 1

Related Questions