Rg786
Rg786

Reputation: 305

ProgressBar issues : copying a folder to destination updates

Below i have the code for the copying of a directory for the import function of the application, i want to update the progressbar and label accoridngly. Ive just been reading up about the background worker and that it would be suited to this particular case as well. Heres the code:

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

Upvotes: 0

Views: 1227

Answers (1)

jmelhus
jmelhus

Reputation: 1140

You are setting the value of the progressbar to zero in each iteration of the foreach. Move pbProcessFiles.Value = 0; out of the foreach statement...

Upvotes: 1

Related Questions