在asp.net中按日期过滤DirectoryInfo文件

时间:2011-10-09 16:31:09

标签: asp.net vb.net date datagrid directoryinfo

我使用指定路径(DirectoryInfo)中的文件填充数据网格控件 我想根据用户指定的日期范围(开始日期和结束日期)过滤文件。

在搜索S / O时,我发现this帖子,但我在DateComparer上收到错误(“'DateComparer'是一种类型,不能用作表达式。”

有关如何按日期过滤的其他任何建议?

这是我的代码:

        Dim dirInfo As New DirectoryInfo(strDirectoryPath)
        Dim dStartDate As DateTime = "03/01/2011"
        Dim dEndDate As DateTime = "6/30/2011"
        Dim Files As FileInfo = dirInfo.GetFiles().Where(Function(Files) Files.CreationTime >= (dStartDate) AndAlso Files.CreationTime <= dEndDate)

            datagrid.DataSource = Files
            datagrid.DataBind()

1 个答案:

答案 0 :(得分:2)

DateTime your_start_date = new DateTime(2011,1,1);
DateTime your_end_date = new DateTime(2011,10,1);
FileInfo [] files = new DirectoryInfo(@"c:\").GetFiles().Where(x=>x.CreationTime>=(your_start_date) && x.CreationTime<=(your_end_date)).ToArray();

foreach(var item in files)
{
 Console.WriteLine(item.Name);
}

在我的测试用例上打印出来:

copy_one.jpg
copy_one_one.jpg
copy_one_one_one.jpg
hiberfil.sys
one.jpg
pagefile.sys
PcapDotNet.snk

UPDATE(VB版):

Dim your_start_date As New DateTime(2011, 1, 1)
Dim your_end_date As New DateTime(2011, 10, 1)
Dim files As FileInfo() = New DirectoryInfo("c:\").GetFiles().Where(Function(x) x.CreationTime >= (your_start_date) AndAlso x.CreationTime <= (your_end_date)).ToArray()

For Each item As FileInfo In files
    Console.WriteLine(item.Name)
Next