FileSystemWatcher - can't do this without API?

I want to check if several folders receive new files and then process them. This works great, I declared FileSystemWatcher and installed EventHandler. Now everything works fine, and if I create a new file there, he notices it.

Then I noticed that when I insert a file, it does not notice it. I already searched on Google and I read that this is not possible using the built-in FileSystemWatcher. So I thought of an API to handle this, but I really have no idea how to handle this or where to start. This program is one for work, so I really need it. I appreciate any help, links or anything else to handle this.

Thank! if something is not clear, avoid Downvote and ask me;)

+4
source share
2 answers

The following work completed as expected (.net v4.5). Insertion into the directory raises the Change event.

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim fw As New FileSystemWatcher
    fw.Path = "c:\Temp"
    fw.Filter = "*.*"
    fw.IncludeSubdirectories = False

    AddHandler fw.Created, New FileSystemEventHandler(AddressOf FileWatcherFileChange)
    AddHandler fw.Deleted, New FileSystemEventHandler(AddressOf FileWatcherFileDeleted)
    AddHandler fw.Renamed, New RenamedEventHandler(AddressOf FileWatcherFileRenamed)
    AddHandler fw.Error, New ErrorEventHandler(AddressOf FileWatcherError)

    fw.EnableRaisingEvents = True

End Sub

Private Sub FileWatcherFileChange(ByVal source As Object, ByVal e As FileSystemEventArgs)

    MsgBox("Change")

End Sub

Private Sub FileWatcherFileDeleted(ByVal source As Object, ByVal e As FileSystemEventArgs)

    MsgBox("Deleted")

End Sub

Private Sub FileWatcherFileRenamed(ByVal source As Object, ByVal e As FileSystemEventArgs)

    MsgBox("Renamed")

End Sub

Private Sub FileWatcherError(ByVal source As Object, ByVal e As System.IO.ErrorEventArgs)

    MsgBox("Error")

End Sub 

Final class

+1
source

There were so many comments, I want you to read this, so I am posting as an answer. Please read this answer from me in another thread in full, including what is written in CodeProject. If what you supply is an important work code, you really need to take the time to read it all.

FileSystemWatcher . - โ€‹โ€‹ , . , .

, FileSystemWatcher . , .

: : fooobar.com/questions/45353/...

+1

Source: https://habr.com/ru/post/1532193/


All Articles