Click here to Skip to main content
15,028,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

My task is to copy the selected format (.tif, tiff, .jpg and .jpeg) files from one location to other location through the application i have developed.

Task is completed successfully but need to add progress bar which should show the count and percentage of files copied to the destination.

Below is the page code:


Copying the files from one location to other with folder threshold
            if (!int.TryParse(textBox3.Text, out int thresholdValue) || thresholdValue < 1)
            {
                // TODO: Display an error message to the user
                return;
            }

            string source = textBox1.Text;
            string destination = textBox2.Text;
            int totalFileCount = 0;
            int currentSubFolder = 0;
            int remainingFileCount = 0;
            string destinationFolder = null;

            ISet<string> extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    ".tif",
    ".tiff",
    ".jpg",
    ".jpeg"
};

            IEnumerable<string> files = Directory
                .EnumerateFiles(source, "*", SearchOption.AllDirectories)
                .Where(f => extensions.Contains(Path.GetExtension(f)));

            foreach (string sourceFile in files)
            {
                if (remainingFileCount == 0)
                {
                    // First file, or the sub-folder is full:
                    currentSubFolder++;
                    destinationFolder = Path.Combine(destination, currentSubFolder.ToString("D3"));
                    if (!Directory.Exists(destinationFolder)) Directory.CreateDirectory(destinationFolder);
                    remainingFileCount = thresholdValue;
                }

                string destinationFile = Path.Combine(destinationFolder, Path.GetFileName(sourceFile));
                File.Copy(sourceFile, destinationFile);
                totalFileCount++;
                remainingFileCount--;   
            }

            MessageBox.Show("All " + totalFileCount + " files are copied");


What I have tried:

i tried adding the progress bar by taking the object but it's still not working.
Posted
Updated 4-Oct-20 19:16pm
Comments
Member 8010354 5-Oct-20 3:31am
   
thank you

1 solution

It's not working because you are moving files on the UI thread - which means it can't update the display until your method ends.

Move your code to actually do the move into a new thread and report progress back to the main thread and it'll work. The easiest way to do that is via a background worker:
C#
private void FrmMain_Shown(object sender, EventArgs e)
    {
    BackgroundWorker work = new BackgroundWorker();
    work.WorkerReportsProgress = true;
    work.DoWork += Work_DoWork;
    work.ProgressChanged += Work_ProgressChanged;
    work.RunWorkerCompleted += Work_RunWorkerCompleted;
    work.RunWorkerAsync();
    }

private void Work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    // Do whatever is necessary after worker complete
    }

private void Work_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
    // Do display updates as files get copied
    }

private void Work_DoWork(object sender, DoWorkEventArgs e)
    {
    // Do the actual file copy and use ReportProgress to update the display.
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900