Example #1
0
        private void btnCopy_Click(object sender, EventArgs args)
        {
            string source = txtSourceDirectory.Text;
            string dest   = txtDestinationDirectory.Text;

            if (!Directory.Exists(source) || !Directory.Exists(dest))
            {
                MessageBox.Show("The source and destinations folder must exist.", "Error");
                return;
            }

            btnCopy.Enabled   = false;
            progressBar.Value = 0;
            progressBar.Style = ProgressBarStyle.Continuous;

            _worker = new BackgroundWorker();

            _worker.DoWork += (o, e) =>
            {
                string[] files = Directory.GetFiles(source);
                for (int i = 0; i < files.Length; ++i)
                {
                    Thread.Sleep(1000);
                    File.Copy(files[i], Path.Combine(dest, Path.GetFileName(files[i])));
                    _worker.ReportProgress((int)((100.0f * i) / files.Length));

                    FileCopyPerformanceCounters.UpdateTotalFiles(i);
                }
            };
            _worker.WorkerReportsProgress = true;
            _worker.ProgressChanged      += (o, e) =>
            {
                this.BeginInvoke((MethodInvoker) delegate
                {
                    progressBar.Value = e.ProgressPercentage;

                    FileCopyPerformanceCounters.UpdatePercentDone(e.ProgressPercentage);
                });
            };

            _worker.RunWorkerCompleted += (o, e) =>
            {
                this.BeginInvoke((MethodInvoker) delegate
                {
                    btnCopy.Enabled   = true;
                    progressBar.Style = ProgressBarStyle.Marquee;
                });
            };

            _worker.RunWorkerAsync();
        }
Example #2
0
        public MainForm()
        {
            InitializeComponent();

            FileCopyPerformanceCounters.Initialize();
        }