ProgressBar问题:将文件夹复制到目标更新

时间:2013-08-02 18:15:50

标签: c# visual-studio-2010 directory copy progress-bar

下面我有复制应用程序导入功能目录的代码,我想更新进度条并按照标签进行标记。我刚刚读了关于后台工作者的内容,并且它也适合这个特殊情况。 下面是代码:

private void importToolStripMenuItem_Click(object sender, EventArgs e)
    {
        lbProcessFiles.Visible = false;
        pbProcessFiles.Visible = false;

        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            string sourcePath = folderBrowserDialog1.SelectedPath;
            string destinationPath = @"Logs\\";

            //Now Create all of the directories
            foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
            {
                lbProcessFiles.Visible = true;
                lbProcessFiles.Text = "Copying Directories";
                pbProcessFiles.Value = 0;
                pbProcessFiles.Maximum = dirPath.Length;

                Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath));

                pbProcessFiles.PerformStep();
            }

            lbProcessFiles.Text = "Now Copying Files";

            //Copy all the files
            foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
            {
                pbProcessFiles.Value = 0;
                pbProcessFiles.Maximum = newPath.Length;
                lbProcessFiles.Text = "Copying Files";

                File.Copy(newPath, newPath.Replace(sourcePath, destinationPath));

                pbProcessFiles.PerformStep();
            }                    
        }
        lbProcessFiles.Text = " Import Complete";
    }

1 个答案:

答案 0 :(得分:1)

您在foreach的每次迭代中将进度条的值设置为零。将pbProcessFiles.Value = 0;移出foreach声明......