按日期查找目录中的文件

时间:2015-09-03 08:04:34

标签: c#

如何在C#中只将文件夹中最后一个日期的文件带到datagridview 我从这个网站得到了一些答案,但是所有这些都给我带来了文件夹中的所有文件我只想要在上次日期创建的文件。 我得到的答案

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();

没关系,但我只想要最后一个日期文件 我希望我的意思很清楚

2 个答案:

答案 0 :(得分:1)

昨天创建的所有文件:

const int perinsert = 50;
foreach (string table in tables)
{
    string[] fields = fieldslist[table].ToArray();
    MySqlDbType[] types = typeslist[table].ToArray();
    bool[] nulls = nullslist[table].ToArray();

    int thisblock = perinsert;
    int rowstotal = theData[table].Count;
    int rowsremainder = rowstotal % perinsert;
    int rowscopied = 0;

    // Do the bulk (multi-VALUES block) INSERTs, but only if we have more rows than there are in a single bulk insert to perform:
    while (rowscopied < rowstotal)
    {
        if (rowstotal - rowscopied < perinsert)
            thisblock = rowstotal - rowscopied;
        // Generate a 'perquery' multi-VALUES prepared INSERT statement:
        List<string> extravals = new List<string>();
        for (int j = 0; j < thisblock; j++)
            extravals.Add(String.Format("(@{0}_{1})", j, String.Join(String.Format(", @{0}_", j), fields)));
        localcmd.CommandText = String.Format("INSERT INTO {0} VALUES{1}", tmptable, String.Join(",", extravals.ToArray()));
        // Now create the parameters to match these:
        for (int j = 0; j < thisblock; j++)
            for (int i = 0; i < fields.Length; i++)
                localcmd.Parameters.Add(String.Format("{0}_{1}", j, fields[i]), types[i]).IsNullable = nulls[i];

        // Keep doing bulk INSERTs until there's less rows left than we need for another one:
        while (rowstotal - rowscopied >= thisblock)
        {
            // Queue up all the VALUES for this block INSERT:
            for (int j = 0; j < thisblock; j++)
            {
                Dictionary<int, object> row = theData[table][rowscopied++];
                for (int i = 0; i < fields.Length; i++)
                    localcmd.Parameters[String.Format("{0}_{1}", j, fields[i])].Value = row[i];
            }
            // Run the query:
            localcmd.ExecuteNonQuery();
        }
        // Clear all the paramters - we're done here:
        localcmd.Parameters.Clear();
    }
}

如果您想以DateTime yesterday = DateTime.Today.AddDays(-1); IEnumerable<FileInfo> filesFromYesterday = new System.IO.DirectoryInfo("path") .EnumerateFiles("*.*", SearchOption.AllDirectories) .Where(file => file.CreationTime.Date == yesterday); 的方式展示它们,可以单程:

DataGridView

答案 1 :(得分:0)

创建DataGridViewInitialize方法并使用answer @Tim Schmelter 并阅读文章Coding Conventions