1

I am trying to transfer large files (5gb~50gb) on my server from to my external harddisk using windows application C#.

Code used to transfer the files:

    public void CopyFile(string source, string dest)
    {
        using (FileStream sourceStream = new FileStream(source, FileMode.Open))
        {
            byte[] buffer = new byte[64 * 1024]; // Change to suitable size after testing performance
            using (FileStream destStream = new FileStream(dest, FileMode.Create))
            {
                int i;
                while ((i = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    destStream.Write(buffer, 0, i);
                    //OnProgress(sourceStream.Position, sourceStream.Length);
                }
            }
        }
    }

But the problem with this code is that when the application runs, my application would just hang there (although file still transfers at a slow speed)

Is there a better method for copying large files from the remote server?

LuxuryWaffles
  • 1,518
  • 4
  • 27
  • 50
  • 3
    You know about `File.Copy` right? Not that it will help with this issue, but you don't need to reimplement it :) – Blorgbeard Jan 09 '14 at 01:21
  • 1
    Anyway, I'd suggest looking at doing the copy in a BackgroundWorker or Thread. – Blorgbeard Jan 09 '14 at 01:22
  • What version of .NET are you using for this and in what type (WPF, WinForms, ASP.NET, Console) of application is this running in? – siva.k Jan 09 '14 at 01:23
  • 1
    Check out http://stackoverflow.com/questions/882686/asynchronous-file-copy-move-in-c-sharp If you don't want your app to "hang" you will need to make it do something else while it is busy moving your bits, like report progress. – brianfeucht Jan 09 '14 at 01:23
  • @siva.k .Net Framwork 4, windows form application – LuxuryWaffles Jan 09 '14 at 01:26
  • @Blorgbeard File.Copy hangs the application too, tried it before got worst results. I will look into threads, thanks – LuxuryWaffles Jan 09 '14 at 01:27

3 Answers3

1

You should do that operation in separate thread instead of the current main application thread because this is a blocking operation and your application will block until the transfer has finished. Have a look at the BackgroundWorker, it runs on a separate thread and you can send progress report back to the main thread which comes in handy and you could even implement progress bar.

Leo
  • 14,625
  • 2
  • 37
  • 55
0

If you're doing winforms, put this in your WHILE loop:

Application.DoEvents();

It won't block (freeze) any more.

Xavier J
  • 4,326
  • 1
  • 14
  • 25
  • Im using a for and foreach loop, it still hangs. Am i putting it in the wrong place? (I put it at the start of the loop) – LuxuryWaffles Jan 09 '14 at 01:32
  • while ((i = sourceStream.Read(buffer, 0, buffer.Length)) > 0) { Application.DoEvents(); //here!!! destStream.Write(buffer, 0, i); //OnProgress(sourceStream.Position, sourceStream.Length); } – Xavier J Jan 09 '14 at 01:33
0

Checkout BITS (Background Intelligent Transfer Service):

http://msdn.microsoft.com/en-us/library/aa363160(v=vs.85).aspx

rheitzman
  • 2,247
  • 3
  • 20
  • 36