4

I want to check out if several folders are receieving new files and then handle that. This works fine, I have declared the FileSystemWatcher and set the EventHandler. Now, everything works fine and if I create a new file there, it notices it.

Then, I noticed that when I paste a file, it does not notice it. I have already searched on Google and I read that it is not possible with the built-in FileSystemWatcher so far. So I thought about API to manage this, but I have actually no idea how to deal with that or where to start. This program is one for a job, so I really need that. I appreciate any help, links or something else to deal with that.

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

Dominic B.
  • 1,897
  • 1
  • 16
  • 31
  • What do you mean with: "when I paste a file, it does not notice it"? Do you mean that there is no event thrown, when you change the content of the file? And: What language are you talking about? C#? – Markus Safar Mar 17 '14 at 18:59
  • 2
    According to http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx, 'The operating system and FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents. If you cut and paste a folder with files into a folder being watched, the FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.'. Also see http://social.msdn.microsoft.com/Forums/vstudio/en-US/d2eebf99-091e-46a2-a4af-81e61a9d9849/filesystemwatcher-will-not-recognize-pasted-files – Mike Cheel Mar 17 '14 at 19:01
  • @MarkusSafar Yep, you can see it like that. It does not see pasting as creating a file, so there is no event for that, I think. I am talking about VB.NET as a customer wish, you can also see that in the tags. MikeCheel Thanks, I will have a look at it. i did not find that part of this article. So it will interpret these actions as renaming? Is that the event I am looking for? – Dominic B. Mar 17 '14 at 19:03
  • Sorry, I didn't see the tag when I saw the question. Maybe you were still tagging? ;-) However - make sure that you set the [NotifyFilter](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.notifyfilter%28v=vs.110%29.aspx) correct. – Markus Safar Mar 17 '14 at 19:11
  • No problem ;) Yes, the filter should be okay. – Dominic B. Mar 17 '14 at 19:19
  • Thanks! Renamed worked. – Dominic B. Mar 17 '14 at 19:50

2 Answers2

1

The following works completed as expected (.net v4.5). A paste into the directory fires 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 

End Class

Rob
  • 3,488
  • 3
  • 32
  • 27
  • Yes, I know, rename is responsible to include pasting etc. The problem is fixed, but I just wrote a comment, so still thanks :) I will close here then – Dominic B. May 03 '14 at 06:27
1

There were so many comments, I want to ensure you read this so I post as an answer. Please read this answer from me in another thread in full, including what is written on CodeProject. If what you are delivering is important work code you should really take the time to read it all.

This FileSystemWatcher class features many surprises. It is a leaky abstraction - it doesn't work in any way like it should. It might work for a while, but it always fails when conditions change.

If I were you I would ditch the FileSystemWatcher class entirely and work with time and date based scans for new files. There may be other classes and pre-made components that can help you with this.

UPDATE: New information: https://stackoverflow.com/a/23704476/129130

Community
  • 1
  • 1
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
  • Well ok, thanks, I will have a look at it, but now the progam is already out. But in future I will see what I can do better. Thanks! – Dominic B. May 03 '14 at 06:25
  • Hope it works out, keep in mind that just installing on a new computer can change the hardware and the disk caching that seemed to interfere with my code. I would keep it running where it is. – Stein Åsmul May 03 '14 at 21:20