vb.net

File copy with progress feedback & ability to cancel using manged .NET only (C# & VB.NET)

While working on TVRenamer I came across the need to give progress feedback for the user and give the user the ability to stop copying and since System.IO.File.Copy or Move doesn't provide any feedback and using the VB.NET copy methods means that this will involve another DLL and might not work on mono supported systems, beside that it doesn't give you control over the UI, so I went on and created my own class.

The idea is to have two streams one to read the source file and the other to write to the destination, while raising an event when progress is made, the class exposed one property which is the buffer size, buffer size will impact performance I found the 3MB is a good buffer size in general.

Here's an example of how to use the class:

oFS.CopyProgress += new EventHandler<FileSystem.CopyProgressEventArgs>(oFS_CopyProgress);
bool success = oFS.CopyFile(sourceFileName, destFileName);
if (success)
{
      lblProgressStatus.Text = "File copied successfully";
}

void fs_CopyProgress(object sender, FileSystem.CopyProgressEventArgs e)
{
 
    pb.Value = (int)(e.percentage * 100);
    Application.DoEvents();
}

Syndicate content