0

I am new to .net programming in C#; Currently I'm developing a project in which I want a functionality that happens when a file is deleted.

I can move this file to my custom folder named "recovery", but I don't know how to do this, please help me.

I want to use SHFileOperation to do this.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Imdad Haider
  • 5
  • 1
  • 7
  • Why not using [System.IO.File.Move(...)](http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx), but SHFileOperation? –  Apr 17 '14 at 06:43
  • Actually, rather use [System.IO.Directory.Move(...)](http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx), as this can move both files and directories. –  Apr 17 '14 at 06:52
  • If you want to automatically copy a file before it is deleted by some other process (as suggested by the answers given here already), then though luck: You are in for some advanced programming tasks. Just for getting you in the right mood, look here: http://stackoverflow.com/questions/10727844/intercept-filesytemcall-for-deletion or here: http://forum.sysinternals.com/deletefile-hook_topic23634.html –  Apr 17 '14 at 13:54
  • Directory.Mode() cannot move to a different volume, e.g. from C:\ to D:\ – Kim Homann Aug 05 '20 at 11:20
  • Why delete the file, instead just move it to the recovery folder for delete operation. – Srikanth Feb 12 '23 at 03:59

1 Answers1

-1

You may use FileSystemWatcher, listen to OnDelete event, then copy to custom folder. http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

  • Unfortunately, it doesn't work. OnDelete is fired after the file is deleted. –  Apr 17 '14 at 13:40