简体   繁体   English

使用进度栏复制不起作用

[英]Copying With Progress Bar not working

I kinda have a problem. 我有点小问题。 I'm trying to make a form that copies stuff from point A to B with a statusbar. 我正在尝试使用状态栏将表单的内容从A点复制到B的表单。 Now the copying works fine but the statusbar just isn't doing anything.. Anyone got any clue? 现在复制可以正常工作,但是状态栏什么也没做。任何人都有线索吗?

public partial class Form4A : Form
{
    public Form4A()
    {
        InitializeComponent();
        OtherSettings();
        BackgroundWorker.RunWorkerAsync(); // Starts wow copying
    }

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string SourcePath = RegistryRead.ReadOriginalPath();
        string DestinationPath = RegistryRead.ReadNewPath();

        if (!Directory.Exists(SourcePath))
        {
            for (int i = 1; i <= 100; i++)
            {
                //Now Create all of the directories
                foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
                    SearchOption.AllDirectories))
                    Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

                //Copy all the files
                foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
                    SearchOption.AllDirectories))
                    File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));


                BackgroundWorker.ReportProgress(i);
            }
        }
    }
    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Change the value of the ProgressBar to the BackgroundWorker progress.
        progressBar1.Value = e.ProgressPercentage;
        // Set the text.
        this.Text = e.ProgressPercentage.ToString();
    }

}

You say: if (!Directory.Exists(DestinationPath)) . 您说: if (!Directory.Exists(DestinationPath)) This means that the loop will never be executed, if the desination path exists. 这意味着,如果存在目标路径,则将永远不会执行循环。 Make sure you delete the DestinationPath before you test your code! 在测试代​​码之前,请确保删除DestinationPath!

EDIT: 编辑:

if (Directory.Exists(SourcePath)) {
    //Now Create all of the directories 
    string[] allDirectories = Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories);
    string[] allFiles = Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);
    int numberOfItems = allDirectories.Length + allFiles.Length;
    int progress = 0;

    foreach (string dirPath in allDirectories) {
        Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
        progress++;
        BackgroundWorker.ReportProgress(100 * progress / numberOfItems);
    }

    //Copy all the files 
    foreach (string newPath in allFiles) {
        File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
        progress++;
        BackgroundWorker.ReportProgress(100 * progress / numberOfItems);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM