2

Does anyone have an example of working with a progress bar while copying a file, or can direct me to a place where this question has been asked?

    private void Transferfiles(DirectoryInfo source, DirectoryInfo target)
    {
        int e = 0
        if (Directory.Exists(target.FullName) == false)
        {

                Directory.CreateDirectory(target.FullName);

        }
        foreach (FileInfo eachhfile in source.GetFiles())
        {
                eachhfile.CopyTo(Path.Combine(target.ToString(), eachhfile .Name));
                BytesToKilobytes += ((eachhfile .Length / 1024) / 1024);
                e = BytesToKilobytes ;
                backgroundWorker1.ReportProgress(e); 
        }
        foreach (DirectoryInfo SubDirectory in source.GetDirectories())
        {
                DirectoryInfo newTargetDirectory =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                Transferfiles(SubDirectory, newTargetDirectory );
        }
    }

The above is the code I have used so far. It works but doesn't really give me what I want. I am looking for a way to make the progress bar update as the file is copying, so that the progress bar will keep moving until the file has finished copying.

AndyUK
  • 3,933
  • 7
  • 42
  • 44
yhusoonpoint
  • 45
  • 1
  • 9
  • okay so what is this giving you now? Can you describe it's current behavior? Also you should never compare a boolean to a boolean. It's a boolean already just not it if you have to – rmn36 Sep 15 '15 at 19:39
  • when it finish copying the file, the progress bar increases intermediately to the value. it does not move as the file is copying. – yhusoonpoint Sep 15 '15 at 19:40
  • Hmm, maybe not. `backgroundWorker1.ReportProgress(e);` You actually doing that in a background thread? –  Sep 15 '15 at 20:02
  • I'm not posting this as an answer because it is so far from complete, but you probably want to do the actual file copy using the Win32 API CopyFileEx function. This function supports a progress callback. Searching for ".Net CopyFileEx" will help you find at least one .Net wrapper for CopyFileEx. – OldFart Sep 15 '15 at 20:32

3 Answers3

1

You could check the size of the folder you're moving it to and use it as the current value of the progress bar.

DeaDViruS
  • 125
  • 9
0

(probably I misunderstood you, because you want the bar move on even on a single big file? If that is the case, I have no quick solution for you and even Windows does not manage it within file explorer)

MSDN is your friend:

https://msdn.microsoft.com/de-de/library/system.windows.forms.progressbar(v=vs.110).aspx

private void CopyWithProgress(string[] filenames)
    {
        // Display the ProgressBar control.
        pBar1.Visible = true;
        // Set Minimum to 1 to represent the first file being copied.
        pBar1.Minimum = 1;
        // Set Maximum to the total number of files to copy.
        pBar1.Maximum = filenames.Length;
        // Set the initial value of the ProgressBar.
        pBar1.Value = 1;
        // Set the Step property to a value of 1 to represent each file being copied.
        pBar1.Step = 1;

        // Loop through all files to copy.
        for (int x = 1; x <= filenames.Length; x++)
        {
            // Copy the file and increment the ProgressBar if successful.
            if(CopyFile(filenames[x-1]) == true)
            {
                // Perform the increment on the ProgressBar.
                pBar1.PerformStep();
            }
        }
    }
Falco Alexander
  • 3,092
  • 2
  • 20
  • 39
0

You will need to implement the copy code yourself (you can't use FileCopy or similar commands). For an example of how you'd do this, see this article.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80