My main question is: How to do file operations with the user being able to cancel them in the middle.
I know I can use the Microsoft.VisualBasic.FileIO namespace like: FileSystem.DeleteFile(...) but that only gives me an option for one file. I would like to delete many files together. (And the same goes for copying…)
Note: I need the user to be able to cancel the operation if needed even in the middle of copying a large file, not just between files. I know I can do that with unmanaged code (-CopyFileEx) but I’m looking for a managed way to do that, whether or not it uses the native Windows dialog.
EDIT: I’ve been suggested ‘wrappers’ for unmanaged code and ‘manual’ copying and deleting (in the answers below). Is there an in-built way (like the VisualBasic namespace stuff – but for multiple files) that I’m missing?
In terms of copying, you will need to do this manually by copying the file in chunks and each time you process a chunk you’ll check if the user set a cancel flag.
For delete, your best bet is you process the file delete one at a time and do the same type of process I describe above.
Advantage of this is that it will work regardless of operating system. It will work regardless of the types of files. It will work regardless of whatever threading you are implementing.