Checking for new files in a folder

血红的双手。 提交于 2021-02-10 05:32:30

问题


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?

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

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

Thanks in advance


回答1:


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

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



回答2:


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.

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.



来源:https://stackoverflow.com/questions/26015325/checking-for-new-files-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!