在目录中搜索在两个日期之间创建的文件

时间:2017-04-13 12:09:25

标签: vb.net file date search directory

我正在尝试搜索指定文件夹中的所有文件,这些文件是在两个日期之间创建的。

日期从两个日期时间选择器中选择。

我在C#中找到了这个:

var directory = new DirectoryInfo(your_dir);
var files = directory.GetFiles()
.Where(file => file.LastWriteTime >= DateTimePicker1
&& file.LastWriteTime <= datetimerpicker2);

我已改为:

Dim Directory = New DirectoryInfo("C:\Test")
Dim path As String
path = (TextBox1.Text)
Dim files = Directory.GetFiles().Where(File.GetLastWriteTime(path) >= DateTimePicker1 And File.GetLastWriteTime <= datetimerpicker2)

我显然有些错误。

结果(文件路径和名称)将发布到Listbox1。我还希望能够通过单击其列表框条目打开文件路径,但这是我尚未开始研究的内容。

有关VB.NET解决方案的任何想法吗?

3 个答案:

答案 0 :(得分:4)

您必须使用DateTimePicker.Value属性。也可以使用VB.NET而不是C#语法:

Dim files = From file in directory.EnumerateFiles()
            Where file.LastWriteTime >= DateTimePicker1.Value AndAlso file.LastWriteTime <= Datetimerpicker2.Value 

答案 1 :(得分:0)

使用Tim Schmelter的帮助,我得到了所需的结果;

    ListBox1.Items.Clear()
    Dim csvFiles = From csv In Directory.EnumerateFiles(TextBox1.Text, "*.*", IO.SearchOption.AllDirectories)
                   Where File.GetCreationTime(csv).Date >= DateTimePicker1.Value AndAlso File.GetCreationTime(csv) <= DateTimePicker2.Value

    For Each csvPath In csvFiles
        ListBox1.Items.Add(csvPath)
    Next

End Sub

答案 2 :(得分:0)

这个怎么样

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim path As String = TextBox1.Text
    LoadListBox(path)
End Sub

Private csvFiles As List(Of String)
Private Sub LoadListBox(path As String)
    Dim di As New IO.DirectoryInfo(path)
    di.EnumerateFiles("*.*", IO.SearchOption.AllDirectories)
    csvFiles = (From csv In di.EnumerateFiles("*.*", IO.SearchOption.AllDirectories)
                Where csv.CreationTime.Date >= DateTimePicker1.Value.Date AndAlso
                 csv.CreationTime.Date <= DateTimePicker2.Value.Date
                 Select csv.FullName).ToList

    ListBox1.DataSource = csvFiles

End Sub