How can you detect when files are deleted in Windows Explorer?

I have a WinForms application with TreeView. The user can drag and drop files from WindowsExplorer to TreeView, and then they can drag and drop files back to WindowsExplorer, which affects copies of files anywhere where files were deleted. I try to do this if the files already exist in the directory where the files are deleted, I want to rename the files / folders copied in advance so that there is no collision.

This is how I copy files to WindowsExplorer. In the treeView ItemDrag, I go through the nodes of the selected node and then pack it into an array. Then I use this code:

var dataObject = new DataObject(DataFormats.FileDrop, files.ToArray()); dataObject.SetData(DataFormats.StringFormat, dataObject); DoDragDrop(dataObject, DragDropEffects.Copy); 

This works well, but as soon as it goes to Windows Explorer, it is out of my hands. How do I know when and where files are copied to, and intercept them to make changes? Is it possible?

+4
source share
4 answers

A drag and drop guide is a great article to do what you are trying to achieve.

EDIT2: It seems like there is a C ++ article for the same in CodeProject. But I could not find a way to do this using C #.

+1
source

I do not think that's possible.

0
source

AFAIK, there is no way to find out the drop target (in the destination folder of your case). You can see the CFSTR_FILENAMEMAP shell clipboard format, but still in this case you can only provide name mappings before (or in the process) drag-and-drop.

Also note that, by default, a DataObject in .net has limited shell support. Therefore, if you need to use the format mentioned above, you need to write your own implementation of IDataObject (or take someone else's implementation, a good example using drag-and-drop shell material here )

0
source

Instead of putting the file names in the data object, create a temporary file with a unique / distinguishable name and instead put that name in the data list of the data object (this file may be empty or contain some information that you may need) Use FileSystemWatcher (view the entire disk) to detect a crash (set the filter to a temporary file name, set IncludeSubDirectories to true and set the path to the root directory of the disk to view.) Initiate DoDragDrop. After deleting a unique / easily distinguishable file, FileSystemWatcher can tell you where it was deleted, and you can do whatever you need (for example, delete the deleted temporary file and replace with the ones you originally wanted to delete .- from an ideal solution, but can help. Better yet, it might give someone the idea to come up with a better one!)

One drawback is that you really donโ€™t know in which drive someone can delete the file, and you may have to configure an observer for several drives. And if you skip the disk (or network path), then the problems .....! Do not forget to get rid of observers after the fall.

However, there must be a better way. for example, when dragging a file from the zip folder. The file is extracted only after deletion.

0
source

All Articles