vb.net 检查文件夹中的新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26015325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:16:14  来源:igfitidea点击:

Checking for new files in a folder

vb.netfilesystemwatcher

提问by John

I need to monitor a folder to see when new files are created and then have the file processed and then archived.

我需要监视文件夹以查看何时创建新文件,然后处理文件然后存档。

Its the actual detecting of new files i'm struggling with...I understand that I need to be looking at the FileSystemWatcher thing, but was wondering if anyone knows of any examples of its usage in this way to get me started?

它是我正在努力处理的新文件的实际检测......我知道我需要查看 FileSystemWatcher 的东西,但想知道是否有人知道以这种方式使用它的任何示例来让我开始?

Say my folder is "C:\Temp\", I need to know as soon as any file with a ".dat" extension appear.

假设我的文件夹是“C:\Temp\”,一旦出现任何带有“.dat”扩展名的文件,我就需要知道。

Sorry for the vague question, I just havent been able to find what I'm looking for with various google searches.

抱歉这个含糊不清的问题,我只是无法通过各种谷歌搜索找到我要找的东西。

Thanks in advance

提前致谢

回答by

You can use FileSystemWatcher Classfor this: It Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

您可以为此使用FileSystemWatcher 类它侦听文件系统更改通知并在目录或目录中的文件发生更改时引发事件。

Imports System
Imports System.IO
Imports System.Diagnostics

Public watchfolder As FileSystemWatcher
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    watchfolder = New System.IO.FileSystemWatcher()
    watchfolder.Path = "d:\pdf_record\"
    watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
                               IO.NotifyFilters.FileName
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
                               IO.NotifyFilters.Attributes
    AddHandler watchfolder.Changed, AddressOf logchange
    AddHandler watchfolder.Created, AddressOf logchange
    AddHandler watchfolder.Deleted, AddressOf logchange
    AddHandler watchfolder.Renamed, AddressOf logrename
    watchfolder.EnableRaisingEvents = True
End Sub


 Private Sub logchange(ByVal source As Object, ByVal e As  _
                        System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            MsgBox("File " & e.FullPath & _
                                     " has been modified" & vbCrLf)
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Created Then
            MsgBox("File " & e.FullPath & _
                                      " has been created" & vbCrLf)
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
            MsgBox("File " & e.FullPath & _
                                     " has been deleted" & vbCrLf)
        End If
    End Sub
    Public Sub logrename(ByVal source As Object, ByVal e As  _
                            System.IO.RenamedEventArgs)
        MsgBox("File" & e.OldName & _
                         " has been renamed to " & e.Name & vbCrLf)
End Sub

回答by John

So I have managed to get this working how I wanted and figured I'd share it incase anyone is ever after the same thing.

所以我已经设法让这个工作按照我想要的方式工作,并认为我会分享它,以防万一有人追求同样的事情。

Using this guide [http://www.dreamincode.net/forums/topic/150149-using-filesystemwatcher-in-vbnet/]as a reference, I've added a FileSystemWatcher component to my form.

使用本指南 [ http://www.dreamincode.net/forums/topic/150149-using-filesystemwatcher-in-vbnet/]作为参考,我在表单中添加了一个 FileSystemWatcher 组件。

I use the following to hardcode the directory I want to monitor:

我使用以下代码对要监视的目录进行硬编码:

    Public Sub agent_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Shown
        Fsw1.Path = "C:\temp"
    End Sub

I use the following to add the full path of files created to a listbox...

我使用以下内容将创建的文件的完整路径添加到列表框...

Private Sub fsw1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles Fsw1.Created
    listbox_PendingJobs.Items.Add(e.FullPath.ToString)
End Sub

This works exactly how I want in terms of detecting new files in a folder. Now i'm going to drop a background worker in which is kicked off by a timer at 5 minute intervals to work through and 'process' the entries in the listbox if found.

在检测文件夹中的新文件方面,这正是我想要的。现在,我将删除一个后台工作人员,该工作人员以 5 分钟的间隔由计时器启动,以完成并“处理”列表框中的条目(如果找到)。