Copy File With Update Progress in .Net

The process of copying a file in .Net is of course very simple and straightforward.

//in a real world solution, use Environment.SpecialFolders instead of hard coding c drive letter
string inputpath = "C:\\in\\";
string outpath = "C:\\out\\";
string filename = "foo.txt";
System.IO.File.Copy(inputpath + filename, outputpath + filename, true);
//file copied!.. but if foo was very large, this block of code following would be delayed execution until the copy completes..

If you want to copy multiple files and update the end user on the progress of a large file copy, or copy files asynchronously while performing other tasks in your application, you’re getting into slightly hairy territory.

There are many ways to achieve this with varying results, and you will often see many application installers and various other large file copy processes inaccurately estimate remaining time and incorrectly report progress, or lock up close to the end.

To correct this, my advice would be to go back to basics – copying bytes using FileStream. See example below.

(applies to winform since update progress is displayed to user, but can easily be adapted to console or web)

using System.IO;

 public class ThreadedFormFileCopy : Form

        // Class to report exception {
        private class UIError
            public UIError(Exception ex, string path_)
                msg = ex.Message; path = path_; result = DialogResult.Cancel;
            public string msg;
            public string path;
            public DialogResult result;

        #region "winforms specific"
        public ThreadedFormFileCopy()

        private void btnCopy_Click(object sender, EventArgs e)

        #region "variable declarations"
        FileInfo currentfile { get; set; } //keep track of current file being copied
        long maxbytes { get; set; } //shared with file agent, background worker and form thread to keep track of bytes

        private System.Timers.Timer fileagent; //we'll be using this thread to update the progress bar as bytes are copied

        //would also declare OnChange delegate and progresschange object here as well if byte level progress not needed and just want to update UI for each file copied
        private BackgroundWorker mCopier;
        private delegate void CopyError(UIError err);
        private CopyError OnError;

        public void StartCopy()
            LoadFileAgent(); //instantiate and update file agent parameters such as internal and elapsed handler
            PrepareBackgroundWorker(); //instantiate and update background worker parameters and handlers
            UpdateFormLabels(false); //change labels on form to indicate copy process has begun
            ToggleBackgroundWorker(); //start background worker, can be used with a button or UI element to cancel as well

        private void LoadFileAgent()
            fileagent = new System.Timers.Timer();
            fileagent.Interval = 100; //1ss
            fileagent.Elapsed += new System.Timers.ElapsedEventHandler(FileAgent_Process);

        private void FileAgent_Process(object source, System.Timers.ElapsedEventArgs e)
                label1.Text = currentfile.Name;
                progressBar1.Value = (int)(100 * maxbytes / currentfile.Length);
            catch (Exception ex)
                MessageBox.Show(ex.ToString()); //may want to include in debug only

        private void Copier_DoWork(object sender, DoWorkEventArgs e)
            string[] allowextensions = { ".wmv", ".flv", ".f4v", ".mov", ".mp4", ".mpeg", ".mpg", ".mp3", ".wma" }; //etc, videos can get big and might want progress..
            List<FileInfo> files = new List<FileInfo>();

            //don't forget your Environment.SpecialFolder
            string path = "\\path_containing_files\\";
            DirectoryInfo dir = new DirectoryInfo(path);
            foreach (string ext in theExtensions)
                FileInfo[] folder = dir.GetFiles(ext, SearchOption.AllDirectories);
                foreach (FileInfo file in folder)
                    if ((file.Attributes & FileAttributes.Directory) != 0) continue;
                    maxbytes += file.Length;

            //and now for the good stuff
            int bytesread = 0;
            foreach (FileInfo file in files)
                    currentfile = file;

                    string outputpath =  "\\destination_path\\";

                    FileInfo destfile = new FileInfo(outputpath + file.Name);

                    byte[] buffer = new byte[4096]; //4MB buffer
                    FileStream fsread = file.Open(FileMode.Open, FileAccess.Read);
                    FileStream fswrite = destfile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    maxbytes = 0;

                    while ((bytesread = fsread.Read(buffer, 0, buffer.Length)) > 0)
                        fswrite.Write(buffer, 0, bytesread);
                        maxbytes = maxbytes + bytesread;
                    System.IO.File.SetAttributes(outputpath + file.Name, FileAttributes.Normal);

                catch (Exception ex)
                    UIError err = new UIError(ex, file.FullName);
                    this.Invoke(OnError, new object[] { err });
                    if (err.result == DialogResult.Cancel) break;
                //could update bytes here also


        private void PrepareBackgroundWorker()
            mCopier = new BackgroundWorker();
            mCopier.DoWork += Copier_DoWork; //all the heavy lifting is done here
            mCopier.RunWorkerCompleted += Copier_RunWorkerCompleted;
            mCopier.WorkerSupportsCancellation = true;
            //if you wanted to add an onchange event for the background worker, you could do this here and it would fire after each complete copy, though not necessary this is viable alternative for medium file sizes
            OnError += Copier_Error;

        private void UpdateFormLabels(bool copying)
            label1.Visible = copying;
            progressBar1.Visible = copying;
            label1.Text = "Starting copy...";
            progressBar1.Value = 0;

        private void ToggleBackgroundWorker()
            bool copying = true;
            if (copying)

        private void Copier_Error(UIError err)
            string msg = string.Format("Error copying file. Details: " + err.Message);
            err.result = MessageBox.Show(msg, "Copy error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            Environment.ExitCode = 1;
            Application.Exit(); //log error, do something, close, or continue if it's not critical or used for long unattended copies

        private void Copier_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            label1.Text = "Complete!";
            progressBar1.Value = 100;
            this.Invoke(new MethodInvoker(delegate { FinishedCopying(); }));

        private void FinishedCopying()
            //execute something when worker finished


StackOverflow, “Read/Write bytes in Chunks”,
Java2s, “Write Bytes using Filestream”,
MSDN, “FileStream.WriteByte”,
MSDN, “FileStream.ReadByte”,, “Read Binary Files into a Buffer”,
Java2s, “Read into a Buffer”,
StackOverflow, “Asynchronous stream read/write”,, “Background Worker Progress Changed and Progress Bar”,
StackOverflow, “Copy Stream to byte array”,


About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on May 17, 2011, in Programming & Development and tagged , , , , , , , , . Bookmark the permalink. 8 Comments.

  1. The Line: “byte[] buffer = new byte[4096]; //4MB buffer” should be more like new byte[4096 * 1024 * 1024] which should speed things up a bit.

  2. not working sorry

  3. version ?

  4. Hey Ronnie, great piece of code all working greate but just missing the UIError object though?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: