Jamie
Jamie

Reputation: 10886

file move loop and show progressbar

How could I show a progress bar when moving a file?

Currently I've got this:

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

But because File.Move(pathFrom, pathTo); is not a loupe I've got no Idea how to do this.

Upvotes: 1

Views: 218

Answers (1)

Cee McSharpface
Cee McSharpface

Reputation: 8726

Consider using the SHFileOperation API from shell32. A working solution is described in this answer: How to bring up the built-in File Copy dialog? It works for copy as well as for move and delete.

Upvotes: 2

Related Questions