Abubaraa
Abubaraa

Reputation: 9

Finding files in a directory by date

how can I bring only files made in last date from folder to datagridview in C# I get some answers from this site but all of them bring me All files from folder I want Only the files created in last date. The answers that I get

1-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.LastWriteTime.ToString("yyyy/MM/dd");
            //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
            //{ }

        }



2-dataGridView1.DataSource = new System.IO.DirectoryInenter code herefo(@"Path").GetFiles();

It is ok but I want only last date files I hope it's clear what I mean

Upvotes: 0

Views: 106

Answers (2)

Denis Bubnov
Denis Bubnov

Reputation: 2785

Create DataGridViewInitialize method and use answer @Tim Schmelter And read article Coding Conventions

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460068

All files which were created yesterday:

DateTime yesterday = DateTime.Today.AddDays(-1);
IEnumerable<FileInfo> filesFromYesterday = new System.IO.DirectoryInfo("path")
    .EnumerateFiles("*.*", SearchOption.AllDirectories)
    .Where(file => file.CreationTime.Date == yesterday);

If you want to show them in your DataGridView, one way:

foreach(FileInfo file in filesFromYesterday)
{
    dataGridView1.Rows.Add(file.FullName, file.Length, file.CreationTime.ToShortDateString());
}

Upvotes: 1

Related Questions