7

Is it possible to access a file before it's deleted when using FileSystemWatcher.OnDeleted event?

I'm storing some data about the document itself in its metadata and i need that info before it's deleted.

Any Ideas how to accomplish this with or without FileSystemWatcher if it's even possible ?

Update ://

I realized that storing the data in the file is bad as i cannot access it when file is deleted.

Solution : rewrite my app to store the data in a local database(sqlite/xml or something like that) as i get the full path and name when the file is being created/renamed/updated/deleted it would be easier to update the database record for the file.

Thanks to all for the ideas and suggestions!

Nikola Sivkov
  • 2,812
  • 3
  • 37
  • 63
  • I almost want to say no you cannot because the event fires once the file has been deleted. I do not believe the FSW can know of the event until after it has completed, since that would mean a pre-delete even would need to be fired and captured, and then allow for you to create a lock on the file. – Mike McMahon May 04 '12 at 22:40
  • I don't think that's possible with just `FileSystemWatcher`. I'm not sure if Windows allows a lower-level hook (though I doubt it.) – dlev May 04 '12 at 22:40
  • You would get the fullname from the eventhandler. Use it to restore the file, make it invisible, read it, and delete it. hackish and not foolproof, but still something: http://stackoverflow.com/questions/2837307/recovering-deleted-file-on-windows – nawfal May 04 '12 at 22:48
  • @nawfal that won't work. You will get a `FileNotFoundException` if you try to access the file in the event handler. – GETah May 04 '12 at 22:49
  • @GETah yes, that is why I was asking him to restore the file from recyclebin, read it and then delete again. From the eventhandler he would get the name and associated properties of the file for sure! just that he cant access the file itself – nawfal May 04 '12 at 22:51
  • Is it possible in Win32 to create a hard link to a file? If so, you could hardlink the file to another (temp) file, read the temp file, then delete it; both files should then have been deleted by the OS by then. – David R Tribble May 04 '12 at 22:58
  • @Loadmaster but he doesnt know before being deleted, that's the issue here. So which file to hardlink here? – nawfal May 04 '12 at 23:08

2 Answers2

2

Is it possible to access a file before it's deleted when using FileSystemWatcher.OnDeleted event?

The event is triggered after the file deletion not before, so you won't be able to access the file when this event is raised.

Any Ideas how to accomplish this if it's even possible ?

I would use the OnChanged event instead, which is fired every time the file changes. Basically, you read the file metadata every time the file changes. This can be a bit cumbersome if the file gets updated very often but should allow you to have the latest metadata before the file is removed.

GETah
  • 20,922
  • 7
  • 61
  • 103
  • OnChanged need not necessarily fire before OnDelete! – nawfal May 04 '12 at 22:44
  • also there is no guarantee the file will be changed before it's deleted. – Nikola Sivkov May 04 '12 at 22:46
  • @Aviatrix If the file is not changed before its deleted than there is nothing to read from it. – GETah May 04 '12 at 22:48
  • if the file is not changed and deleted, still there are stuffs to read from it. This solution wont work actually – nawfal May 04 '12 at 22:53
  • @nawfal Except the file descriptor data that can be retrieved on `OnCreate` event, I do not see what the OP can read from an empty file :) – GETah May 04 '12 at 22:55
  • @GETah file isn't empty , it's downloaded , but i see your point , i could store the data in a list in the app and then access the info about the file from that info – Nikola Sivkov May 04 '12 at 22:57
  • He can still read if you do not assume the file is fresh and empty. What about the case of existing files? – nawfal May 04 '12 at 22:58
  • @nawfal They could always do an initial scan on startup to capture files that are already there. – Paul Phillips May 04 '12 at 23:01
  • @PaulPhillips all those are possible, but how do we dare to assume the initial scanning etc etc are all just a breeze? You know never know the number of files or if the files are already existing ones or not or anything abt OP's files. The safest way here is to give one general approach which is what this answer fails to give. – nawfal May 04 '12 at 23:06
  • @nawfal We're answering other people's technical problems over the internet. If you don't make any assumptions, you'll never be able to answer anything. – Paul Phillips May 04 '12 at 23:13
  • @PaulPhillips that's not entirely right for programmers. But yeah seldom we need to, but with an element of enquiring, not with a stamp of authority. Anyhow the disucssion I was having here was nothing related to the work around you suggested here which is welcome btw :) – nawfal May 04 '12 at 23:18
  • @nawfal PaulPhillips is right. If you are a programmer then you should not look for a fully cooked backed solution. You should be looking for elements that will help you out build a solution – GETah May 05 '12 at 08:58
0
FileSystemWatcher1 = Your Main Watcher.
FileSystemWatcher2 = RecycleBin Watcher

If the FileSystemWatcher1 Deleted file == the FileSystemWatcher2 Created File
{
    //Do what you want with the FileSystemWatcher2.FullPath
}
acostela
  • 2,597
  • 3
  • 33
  • 50