0

I have a very large file in GBs. I am using File.Copy() function in C# win Form (not a WPF).

But the form is hanging as the process if working on the copying of the file. I was looking for a solution that will help me track the progress of the File.Copy(). But I do not want to complicate the progress with customized stuff.

Please suggest me what I can do. I am new to Forms and C#. I hope I will get a solution.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 1
    Does [this question](https://stackoverflow.com/questions/6044629/file-copy-with-progress-bar) help you with your task? – vonludi Jul 01 '20 at 12:38
  • https://stackoverflow.com/questions/882686/asynchronous-file-copy-move-in-c-sharp – Rand Random Jul 01 '20 at 12:39
  • But I want use File.Copy(). I do not want to customize things and make it complicated. As the file should remain intact and through any process it will loose it. Please help me. – Jaffer Wilson Jul 01 '20 at 12:40
  • it is byte file – Jaffer Wilson Jul 01 '20 at 12:40
  • 2
    *But I want use File.Copy().* so you can't customize this – Selvin Jul 01 '20 at 12:41
  • yes i cannot. so i look for solution – Jaffer Wilson Jul 01 '20 at 12:41
  • @RandRandom it is good suggestion. but what if I want to show progress? – Jaffer Wilson Jul 01 '20 at 12:42
  • Any solution guys? – Jaffer Wilson Jul 01 '20 at 12:48
  • 1
    In the first comment. – Ralf Jul 01 '20 at 12:48
  • There is no "take the simplest class and do something more complex" solution. There normally never is. You need to use a class that can do that like a Filestream. – Ralf Jul 01 '20 at 12:49
  • I tried the first comment answer. It is copying the file successfully but not showing any progress? Please guide me.: https://stackoverflow.com/a/6055385/4948889. I have called the `object.copy()`. Had I done it right or what I missed? – Jaffer Wilson Jul 01 '20 at 13:13
  • @JafferWilson do not use the accepted solution. Use [the second one](https://stackoverflow.com/a/19755317/2748412). It actually use the implemented framework. It does fire a progress change event. You just have to attach to it and show whatever you want with it. – Franck Jul 01 '20 at 13:15
  • @JafferWilson have you read the solution correctly and done what is said there. Especially have you done what is said in the sentence **after** the code? – Ralf Jul 01 '20 at 13:21
  • @Ralf Yes. but I am not understand what is the issue I cannot see the progress bar. – Jaffer Wilson Jul 01 '20 at 13:24
  • @Ralf What is the meaning of subscribe in that solution? Is it like calling the fuction? – Jaffer Wilson Jul 01 '20 at 13:33
  • @JafferWilson You now what a delegate is? – Ralf Jul 01 '20 at 13:34
  • @Ralf I do not know that .. I will read. But what it has to do with the word subscribe? – Jaffer Wilson Jul 01 '20 at 13:36
  • You will now the answer when you now what a delegate is. You are missing to many things knowledge wise so i would advise to not use any code found on the internet until you understand basic language stuff used in it. – Ralf Jul 01 '20 at 13:38
  • You can use something similar to the accepted answer in [File Copy with Progress Bar](https://stackoverflow.com/q/6044629/7444103), but using the Task method shown in the [other question](https://stackoverflow.com/a/36925751/7444103), passing a [Progress](https://learn.microsoft.com/en-us/dotnet/api/system.progress-1) object to update the UI each time a calculated amount of file chunks has been copied – Jimi Jul 01 '20 at 13:58

3 Answers3

2

Your form hanging, because UI stuff (like updating form content, responding to events, moving window, etc) is executed in main thread (so-called STA thread). In order to not hang UI while executing some operation, you should create another thread for your long-running operation. There are a lot of ways to do it, but the simplest will be to use TPL's Task.Run:

void Click(object sender, EventArgs args) 
{
   Task.Run(CopyFile);
}

void CopyFile() 
{
   File.Copy(this.src, this.target);
   // note that since we're in different thread, we cannot interact with UI,
   // so you have to dispatch your operations to UI thread. That can be done just using 
   // Control's Invoke method
   this.Invoke(NotifySuccess);
}

As for displaying progress, it's will be a bit more complicated. Since File.Copy() doesn't support progress reporting, you have to use FileStreams. You can check example for it here: https://stackoverflow.com/a/6055385/2223729

Amadare42
  • 415
  • 3
  • 14
  • Thanks .. But not the exact solution I am looking for. But a good attempt is appreciated. – Jaffer Wilson Jul 01 '20 at 12:57
  • @JafferWilson of course, feel free to investigate further, but you'll find that unfortunatelly there is no way to use `File.Copy` and be able to report copy progress. – Amadare42 Jul 01 '20 at 13:01
1

I have an excellent idea. Why can you just use the windows Copy Dialog? Just add a reference:

Microsoft.VisualBasic

And use the code:

try
  {
    FileSystem.CopyFile(source_path, destination_path, UIOption.AllDialogs);
  } 

catch (Exception ext)
   {
      MessageBox.Show(ext.Message);
   }

I guess this will help.

1

You can use a BackgroundWorker to accomplish this task.

You can also create your own form and show progress in real-time by this way.

This way, you don't need to use the windows default dialog.

Use these two links for reference. Also, lots of material is available on Google so there shouldn't be any problem.

https://www.youtube.com/watch?v=2qQgctSi4iY

Running a method in BackGroundWorker and Showing ProgressBar

D J
  • 845
  • 1
  • 13
  • 27