从目录中获取文件并将其添加到不断更新的列表中。 VB

时间:2013-05-24 10:36:19

标签: .net vb.net

我一直在寻找能够不断查看目录以查看是否有新文件的内容,如果有的话会将该文件名添加到列表中

我有这个,但只有当我点击列表

时它才有效
   Dim JAR As String = "*.jpg"
    Dim DirInfo As DirectoryInfo = New DirectoryInfo(dir)
    Dim Files() As FileInfo = DirInfo.GetFiles(JAR, SearchOption.AllDirectories)
    Dim File As FileInfo
    Const extension As String = ".jpg"
    For Each File In Files
        If (Not list.Items.Contains(File.Name)) Then
            list.Items.Add(File.Name)
            list.Text.Substring(0, ClientList.Text.Length - extension.Length)
            list.Text = list.Items.Count
        End If
    Next (File)

2 个答案:

答案 0 :(得分:3)

我认为你正在寻找FileSystemWatcher Class

  

收听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。

答案 1 :(得分:0)

一个简单的解决方案是检查该文件是否今天创建。

    Dim files As FileSystemInfo
    Dim path As String
    path = "C:\myDirectory\"

    Dim childinfo As New DirectoryInfo(path)

        For Each files As FileSystemInfo In childinfo.GetFileSystemInfos()

            ' Select only files within the day (format: MM/DD/YY)
            ' It does not include old files being kept in the folder

            If files.CreationTime.Date.Equals(Date.Today) Then
                list.Add(path & files.Name)
            End If

        Next

但是看到FileSystemWatcher Class是个不错的选择。