7

Code:

FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path, "*.exe");
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
fileSystemWatcher.EnableRaisingEvents = true;

The Created Event works fine, but the Deleted Event is only firing, when Deleting a Directory/or Exe with SHIFT. But normal-delete (moving to recycle bin) isn't working/firing the event!

How to solve the problem?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
eMi
  • 5,540
  • 10
  • 60
  • 109
  • Does the event fire when you empty the recycle bin after deleting a file? – ChrisWue Nov 08 '11 at 09:06
  • nope, nothing happens, only when deleting the file holding SHIFT – eMi Nov 08 '11 at 09:09
  • Mh, makes sense. Deleting it means moving it to the recycle bin which is a `Rename` event. After that the file is outside of the path you are watching. – ChrisWue Nov 08 '11 at 09:10
  • Moving/Renaming a file to the recycle bin is not deleting. That you click "Delete" to move a file to the recycle bin is purely a shell abstraction, which the file system doesn't know about. – CodesInChaos Nov 08 '11 at 09:15
  • 2
    Weird, it works for me as it should. It fires when files are deleted or "shift + delete". – Michał Powaga Nov 08 '11 at 09:38

4 Answers4

13

I know it's an old question, but I resolved this by adding FileName to the NotifyFilter property of the FileSystemWatcher object.

AlbertVanHalen
  • 632
  • 4
  • 12
9

This is expected behaviour as the file isn't actually deleted: it's moved.

Try attaching to

filesystemWatcher.Renamed

and checking if the file is moved to the Recycle Bin instead.

Finding where the recycle bin actually is in the filesystem is not trivial, mind you. Some code posted by others (untried) is here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5d2be9aa-411c-4fd1-80f5-895f64aa672a/ - and also here: How can I tell that a directory is the recycle bin in C#?

Community
  • 1
  • 1
Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • thats clear and I know this, but how to solve the problem? the object doesn't have a moving event! – eMi Nov 08 '11 at 09:06
  • To expand on this, the recycle bin is actually just a special system directory, usually `C:\$RECYCLER` or `C:\$Recycle.Bin`. It aggregates soft-deleted files into a special tree structure, which can then allow restoration or permenant deletion of those files. – Polynomial Nov 08 '11 at 09:07
  • @JeremyMcGee Interesting, just for clarification, what happens if they shift-delete? – Adam Houldsworth Nov 08 '11 at 09:08
  • In order to detect the movement, you could use the Changed event. When you move a file, it is marked as changed. You should be able to then check if the file is still there using `File.Exists` – Polynomial Nov 08 '11 at 09:09
  • @AdamHouldsworth: that'll be `.Deleted` as the file is then actually removed. – Jeremy McGee Nov 08 '11 at 09:10
  • @AdamHouldsworth - That will issue an actual delete operation on the file, firing the event. Windows Explorer has to explicitly move the files to the recycle bin, rather than doing an actual delete. The normal `kernel32.DeleteFile` call will raise the event. – Polynomial Nov 08 '11 at 09:11
  • Sitting comfortably? http://stackoverflow.com/questions/936397/finding-the-recycle-bin-on-a-local-ntfs-drive – Jeremy McGee Nov 08 '11 at 09:11
  • @Polynomial: According to MSDN http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx a move is a rename event and not a change. – ChrisWue Nov 08 '11 at 09:13
  • @JeremyMcGee Lovely, I was just fostering the comments to flesh the details out but then I noticed it says as much in the OP! lol – Adam Houldsworth Nov 08 '11 at 09:13
  • @Polynomial: the problem is, that when I create a File, it also goes in the changed event, which isn't good.. – eMi Nov 08 '11 at 09:13
  • 1
    @eMi: check to see if the file is actually there – Jeremy McGee Nov 08 '11 at 09:14
  • @ChrisWue - Good point. A file is actually referenced by its full path, so if you change the path it's a rename. – Polynomial Nov 08 '11 at 09:14
1

The solution is to use the following code:

private static void EnableFileWatcherLvl1(string folder, string fileName)
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = folder;
    watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Attributes;
    watcher.Filter = "*.txt";
    watcher.Changed += watcher_Changed;
    watcher.Deleted += watcher_Changed;
    watcher.EnableRaisingEvents = true;
}

static void watcher_Changed(object sender, FileSystemEventArgs e)
{            
    switch (e.ChangeType)
    {
        case WatcherChangeTypes.Changed: { // code here for created file }
             break;
        case WatcherChangeTypes.Deleted: { // code here for deleted file }
             break;
    }       
}

Pay attention to the NotifyFilter property, those are the filters it has to use. And this will trigger for Added and Delete files. I tested this in.Net Framework 4.5.

Joe Almore
  • 4,036
  • 9
  • 52
  • 77
-1

This one will Work.

FileSystemWatcher fsw = new FileSystemWatcher(folderPath);
        fsw.Deleted+= FileSystem_Deleted;
        fsw.EnableRaisingEvents = true;
        fsw.IncludeSubdirectories = true;

static void FileSystem_Deleted(object sender, FileSystemEventArgs e)
    {
        // Write your code
       MessageBox.Show("Deleted Item is " + e.name);

    }
Srikanth
  • 1
  • 1