文件移动循环并显示进度条

时间:2016-11-14 11:33:07

标签: c# progress

移动文件时如何显示进度条?

目前我有这个:

 private void btnMoveFile_Click(object sender, EventArgs e)
        {
            try
            {
                if (pathFrom != null && pathTo != null)
                {
                    if (txtRename.Text != null)
                    {
                        pathTo = pathTo + "\\" + txtRename.Text;
                    }

                    else
                    {

                        pathTo = pathTo + "\\" + Path.GetFileName(pathFrom);
                    }

                    File.Move(pathFrom, pathTo);
                }

                else
                {
                    MessageBox.Show("Kies eerst een bestand.");
                }
            }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 100; i++)
            {
                // Wait 100 milliseconds.
                Thread.Sleep(100);
                // Report progress.
                backgroundWorker1.ReportProgress(i);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            // Set the text.
            this.Text = e.ProgressPercentage.ToString();
        }

但是因为File.Move(pathFrom, pathTo);不是放大镜,我不知道如何做到这一点。

1 个答案:

答案 0 :(得分:2)

考虑使用shell32中的SHFileOperation API。 此答案中描述了一种有效的解决方案:How to bring up the built-in File Copy dialog? 它适用于复制以及移动和删除。