Search code examples
c#multithreadingio

How to copy a file with the ability to cancel the copy?


I’m trying to have the program be able to cancel the copy. Therefore I can’t use Microsoft.VisualBasic.FileIO.FileSystem.CopyFile.

There are some wrappers for CopyFileEx on the web such as here. However, I rather not use something I don’t understand, not wanting any unexpected results (or bugs). Is there a managed way to do this? Or perhaps a wrapper by MS (in something like Windows API CodePack)?


Solution

  • Read the file in small chunks and write it out to the destination. Periodically check whether you've been asked to cancel and if you detect that, stop writing and close the files.