Copy file with progress

Asked

Viewed 1,504 times

6

How I use the C function# File.Copy(string path); to copy a file by incrementing a value in a Progressbar?

  • 1

    Nathan, the HTML support on the site is very basic, prefer to use Markdown: http://answall.com/editing-help

3 answers

4


3

I found a simpler implementation in the response from Anton Semenov.

public delegate void ProgressChangeDelegate(double percentage, ref bool cancel);
public delegate void CompleteDelegate();

class CustomFileCopier {
    public CustomFileCopier(string source, string dest) {
        this.SourceFilePath = source;
        this.DestFilePath = dest;
        OnProgressChanged += delegate { }; //só para facilitar o exemplo aqui
        OnComplete +=  delegate { }; //só para facilitar o exemplo aqui
    }

    public void Copy() {
        byte[] buffer = new byte[1024 * 1024]; // 1MB buffer
        bool cancelFlag = false;
        using (FileStream source = new FileStream(SourceFilePath, FileMode.Open, FileAccess.Read)) {
            long fileLength = source.Length;
            using (FileStream dest = new FileStream(DestFilePath, FileMode.CreateNew, FileAccess.Write)) {
                long totalBytes = 0;
                int currentBlockSize = 0;
                while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0) {
                    totalBytes += currentBlockSize;
                    double percentage = (double)totalBytes * 100.0 / fileLength;
                    dest.Write(buffer, 0, currentBlockSize);
                    cancelFlag = false;
                    OnProgressChanged(percentage, ref cancelFlag);
                    if (cancelFlag == true) {break; } // Delete dest file here
                }
            }
        }
        OnComplete();
    }

    public string SourceFilePath { get; set; }
    public string DestFilePath { get; set; }

    public event ProgressChangeDelegate OnProgressChanged;
    public event CompleteDelegate OnComplete;
}

I put in the Github for future reference.

You can make it more complete. You will have to implement the event subscription, something like:

(percentage, cancelFlag) => { if (OnProgressChanged != null)
                                  OnProgressChanged(percentage, ref cancelFlag); };

and

(percentage, cancelFlag) => { if (OnComplete != null)
                                   OnComplete(percentage, ref cancelFlag); };

You need to rotate in another thread and sign the event OnProgressChanged to receive the copy progress notifications.

If you are using Winforms and want a progress bar ready the . NET has one. I don’t know if it would be compatible with the above class. I didn’t have time to test but I don’t see why not. You would run the PerformStep() within the method that signs the event OnProgressChanged.

Some other options (doesn’t mean I find them good or bad):

1

  • Yes but I am using C# not VB

  • And yet all you have is this: http://prntscr.com/9ko87h

  • 1

    This code is C#, add reference to DLL (Microsoft.VisualBasic.dll), search the folder: ; C:\Windows\Microsoft.NET\Framework\versão.net.xxx ex: C:\Windows\Microsoft.NET\Framework\v2.0.50727

Browser other questions tagged

You are not signed in. Login or sign up in order to post.