Track percentage of file copied

Dec 1, 2011 at 4:11 AM

Hi guys,

I have made another modification to my copy of the project and thought Id post it in case anyone else finds it useful. It may not be the perfect way to do it, but works well for my needs.

Overview: I have a C# desktop application that allows a user to copy files to a device. I run the actual copy process in a BackbgroundWorker.

The requirement: To be able to track the progress of the file copy.

The solution: Track the percentage copied during the file copy. Code below (my additions in red, ellipses denote exclusion of existing code for ease of reading):



namespace OpenNETCF.Desktop.Communication {
    public class RAPI : IRAPI {

        // Progress percentage
        private decimal m_decPercentageDone = 0;
        public decimal PercentageDone {
            get {
                return Math.Round(m_decPercentageDone);

        // Allow progress percentage to be reset
        public void ResetPercentage() {
            m_decPercentageDone = 0;


        public void CopyFileToDevice(string LocalFileName, string RemoteFileName, bool Overwrite)

            // open the local file
            localFile = new FileStream(LocalFileName, FileMode.Open, FileAccess.Read);

            // Get the size of the complete file
            decimal lngFileSize = localFile.Length;

            while (bytesread > 0) {

                // Calculate the percentage copied
                lowsize = CeGetFileSize(remoteFile, ref highsize);
                if (lowsize == uint.MaxValue) {
                } else {
                    m_decPercentageDone = (decimal)((lowsize + (highsize << 32)) / lngFileSize) * 100;

As you can see, if I am unable to get the percentage done I do nothing - I figure that the file copy is more important than the progress reporting... Please feel free to improve this - I am extremely new to C# so I figured most of this out by looking at existing code.

I have implemented this in the CopyFileFromDevice() method as well, but Im sure you can figure it out based on the above (in fact its more simple because you can use localfile.length to get the size of what has already been copied from the device, instead of having to use CeGetFileSize()...).

Happy coding!



Dec 1, 2011 at 4:15 AM

Obviously to display the percentage copied you can use a timer to periodically update a label:

private void tmrCopyProgress_Tick(object sender, EventArgs e) {
    lblDone.Text = m_rapi.PercentageDone.ToString() + "%";