Answered by:
Using ProgressBar control

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.
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)- Proposed as answer by CubePS Tuesday, December 16, 2008 2:12 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, December 18, 2008 7:26 AM
All replies
-
-
-
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)- Proposed as answer by CubePS Tuesday, December 16, 2008 2:12 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Thursday, December 18, 2008 7:26 AM