簡體   English   中英

C# 在復制文件時使用進度條

[英]C# working with progress bar while copying a file

有沒有人有在復制文件時使用進度條的示例,或者可以將我引導到提出此問題的地方?

    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 );
        }
    }

以上是我目前使用的代碼。 它有效,但並沒有真正給我我想要的。 我正在尋找一種在文件復制時更新進度條的方法,以便進度條會一直移動,直到文件完成復制。

您可以檢查要將其移動到的文件夾的大小,並將其用作進度條的當前值。

(可能我誤解了您的意思,因為您希望即使在單個大文件上也可以繼續前進?如果是這種情況,我沒有快速解決方案,甚至 Windows 也無法在文件資源管理器中管理它)

MSDN 是你的朋友:

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

您需要自己實現復制代碼(您不能使用FileCopy或類似命令)。 有關如何執行此操作的示例,請參閱本文

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM