using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace intChanger { public partial class Form1 : Form { BackgroundWorker _bgwCopyOperation = new BackgroundWorker(); public Form1() { InitializeComponent(); _bgwCopyOperation.WorkerReportsProgress = true; _bgwCopyOperation.WorkerSupportsCancellation = true; } private void Form1_Load(object sender, EventArgs e) { } string Dirr; string Dirr2; int no = 0; int Count = 0; int Count2 = 0; private void _bgwCopyOperation_DoWork(object sender, DoWorkEventArgs e, string sourceDirName, string destDirName, bool copySubDirs) { if (no == 0) { no = 1; Dirr = sourceDirName; Dirr2 = destDirName; Count = Directory.GetFiles(Dirr, "*.*", SearchOption.AllDirectories).Length; Count = 100 / Count; } // Get the subdirectories for the specified directory. DirectoryInfo dir = new DirectoryInfo(sourceDirName); if (!dir.Exists) { throw new DirectoryNotFoundException( "Source directory does not exist or could not be found: " + sourceDirName); } DirectoryInfo[] dirs = dir.GetDirectories(); // If the destination directory doesn't exist, create it. if (!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); } // Get the files in the directory and copy them to the new location. FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); file.CopyTo(temppath, false); Count2 = Count2 + 1; //Update progressbar here _bgwCopyOperation.ReportProgress(Count2 * Count); } // If copying subdirectories, copy them and their contents to new location. if (copySubDirs) { foreach (DirectoryInfo subdir in dirs) { string temppath = Path.Combine(destDirName, subdir.Name); //Foo((object)new object[] { (object)"1", (object)"2" })); _bgwCopyOperation.RunWorkerAsync((object)new object[] { (object)subdir.FullName, (object)temppath, (object)copySubDirs }); //_bgwCopyOperation.RunWorkerAsync(subdir.FullName, temppath, copySubDirs); } } } private void button1_Click(object sender, EventArgs e) { if (_bgwCopyOperation.IsBusy != true) { // Start the asynchronous operation. _bgwCopyOperation.RunWorkerAsync((object)new object[] { (object)@"C:\Users\Stefan\Downloads\Portable Python 2.7.15 Basic (x64)\Portable Python 2.7.15 x64", (object)@"C:\Users\Stefan\Documents\Backuppers_Backups\Portable Python 2.7.15 x64", (object)true }); } } private void _bgwCopyOperation_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } } }