none
Using ProgressBar control RRS feed

  • Question

  • I have a backup program(windows application) which copies files from one folder to another.

    I would like to display the progress percent to the user while the files are been copies.

    How can I implement it using the ProgressBar control in my WinForm?

    Thanks.


    Tuesday, December 16, 2008 1:53 PM

Answers

  • Hi,

    Please find the below code snippet to make file  copying progress. Its assuming that progress in same form with a button.

     

            string destDir = @"D:\Backup";  
            private void button1_Click(object sender, EventArgs e)  
            {  
                string sourceDir = @"c:\Test";  
                string[] files = Directory.GetFiles(sourceDir);  
                CopyWithProgress(files);  
            }  
            private void CopyWithProgress(string[] filenames)  
            {  
                progressBar1.Visible = true;  
                progressBar1.Minimum = 1;  
                progressBar1.Maximum = filenames.Length;  
                progressBar1.Value = 1;  
                progressBar1.Step = 1;  
     
                for (int x = 1; x <= filenames.Length; x++)  
                {  
                    if (CopyFile(filenames[x - 1]) == true)  
                    {  
                        progressBar1.PerformStep();  
                    }  
                }  
            }  
     
            private bool CopyFile(string filename)  
            {  
                if (File.Exists(filename))  
                {  
                    try 
                    {  
                        File.Copy(filename, destDir + "\\" + Path.GetFileName(filename));  
                        return true;  
     
                    }  
                    catch (Exception ex)  
                    {  
                        return false;  
                    }  
                }  
                return false;  
            }  
     


    Hope this helps.

    Prakash Subramani (MCAD)
    Tuesday, December 16, 2008 2:12 PM

All replies

  • Declare an variable and increment that value in your Copy method.
    And assign that as the Value for the progressbar.

    Increment speed depends on the rate at which you are copying..
    Tuesday, December 16, 2008 2:10 PM
  • any example?
    Tuesday, December 16, 2008 2:11 PM
  • Hi,

    Please find the below code snippet to make file  copying progress. Its assuming that progress in same form with a button.

     

            string destDir = @"D:\Backup";  
            private void button1_Click(object sender, EventArgs e)  
            {  
                string sourceDir = @"c:\Test";  
                string[] files = Directory.GetFiles(sourceDir);  
                CopyWithProgress(files);  
            }  
            private void CopyWithProgress(string[] filenames)  
            {  
                progressBar1.Visible = true;  
                progressBar1.Minimum = 1;  
                progressBar1.Maximum = filenames.Length;  
                progressBar1.Value = 1;  
                progressBar1.Step = 1;  
     
                for (int x = 1; x <= filenames.Length; x++)  
                {  
                    if (CopyFile(filenames[x - 1]) == true)  
                    {  
                        progressBar1.PerformStep();  
                    }  
                }  
            }  
     
            private bool CopyFile(string filename)  
            {  
                if (File.Exists(filename))  
                {  
                    try 
                    {  
                        File.Copy(filename, destDir + "\\" + Path.GetFileName(filename));  
                        return true;  
     
                    }  
                    catch (Exception ex)  
                    {  
                        return false;  
                    }  
                }  
                return false;  
            }  
     


    Hope this helps.

    Prakash Subramani (MCAD)
    Tuesday, December 16, 2008 2:12 PM