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

        private void btnCopy_Click(object sender, EventArgs e)
        {
            StartCopy();
        }
        #endregion

        #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;
        #endregion


        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)
        {
            try
            {
                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;
                    files.Add(file);
                    maxbytes += file.Length;
                }
            }

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

                    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;
                    }
                    fsread.Flush();
                    fswrite.Flush();
                    fsread.Close();
                    fswrite.Close();
                    fsread.Dispose();
                    fswrite.Dispose();
                    //-------------------
                    System.IO.File.SetAttributes(outputpath + file.Name, FileAttributes.Normal);
                    fileagent.Stop();

                }
                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;
            UpdateFormLabels(copying);
            if (copying)
            {
                mCopier.RunWorkerAsync();
            }
            else
            {
                mCopier.CancelAsync();
            }
        }

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

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

    }

References
StackOverflow, “Read/Write bytes in Chunks”, http://stackoverflow.com/questions/5654298/filestream-read-write-methods-limitation
Java2s, “Write Bytes using Filestream”, http://www.java2s.com/Tutorial/VB/0240__Stream-File/WriteBytesusingFileStream.htm
MSDN, “FileStream.WriteByte”,
MSDN, “FileStream.ReadByte”,
http://msdn.microsoft.com/en-us/library/system.io.filestream.readbyte.aspx
extremevbtalk.com, “Read Binary Files into a Buffer”, http://www.xtremevbtalk.com/showthread.php?t=259085
Java2s, “Read into a Buffer”, http://www.java2s.com/Tutorial/VB/0240__Stream-File/Readintoabuffer.htm
StackOverflow, http://stackoverflow.com/questions/1261570/c-filestream-read-doesnt-read-the-file-up-to-the-end-but-returns-0
StackOverflow, “Asynchronous stream read/write”, http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
xtremevbtalk.com, “Background Worker Progress Changed and Progress Bar”, http://www.xtremevbtalk.com/showthread.php?t=294040
StackOverflow, “Copy Stream to byte array”, http://stackoverflow.com/questions/950513/how-to-copy-one-stream-to-a-byte-array-with-the-smallest-c-code

Advertisements

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. vb.net 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:

WordPress.com Logo

You are commenting using your WordPress.com 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: