views:

61

answers:

2

I want to get an event Before a file is being deleted?

How can I do it?

A: 

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

asawyer
This is not a valid answer. FileSystemWatcher can only throw an event [after a file is deleted](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.deleted.aspx).
Nathan Taylor
The event raised by `FileSystemWatcher` is `Deleted`, past tense. Not `BeforeDeleted`
Rob
Hm sorry about that. I thought I'd done that exact thing with it before but it seems I am mistaken.
asawyer
+3  A: 

As per my answer to this question: How could I prevent a folder from being created using a windows service?

There's no support within the System.IO.FileSystemWatcher, or anything else within the .net Framework (as far as I'm aware) for receiving an event prior to a file being deleted, i.e. at the point the deletion request hits the file system, but prior to it being actioned (I'm assuming here that you want to be able to selectively cancel requests to delete files).

What you'll need to do, if you want to go down this route, is write a File System Filter Driver, which you'll have to write in unmanaged code as far as I'm aware.

Rob
Was just trying to work out how to phrase a similar answer. There are far too many difficult/impossible situations to deal with if one application can arbitrarily delay/prevent another application from performing a delete, and making the notification/actual delete asyncronous raise all kinds of other issues
Damien_The_Unbeliever
@Damien, I had the "I'm sure I've answered this before" feeling. After a search it turned out I'd answered the *opposite*, i.e. file creation. Looks like I've got the full set! ;=) And yeah, allowing a user-mode application to arbitrarily delay/prevent would cause all kinds of chaos
Rob