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();
}

The CopyPorgress event provide feedback and more information such as percentage completed, total bytes, copied bytes...etc.

Hope you find this class useful and please leave a comment if you like it.

Update :
Added VB.NET class and example

AttachmentSize
FileSystem Class (C#)7.89 KB
A Sample Project using the FileSystem Class (C#)12.45 KB
FileSystem Class (VB.NET)6.18 KB
A Sample Project using the FileSystem Class (VB.NET)12.8 KB

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.