I used this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
namespace Windo
You need something like this:
public delegate void ProgressChangeDelegate(double Persentage, ref bool Cancel);
public delegate void Completedelegate();
class CustomFileCopier
{
public CustomFileCopier(string Source, string Dest)
{
this.SourceFilePath = Source;
this.DestFilePath = Dest;
OnProgressChanged += delegate { };
OnComplete += delegate { };
}
public void Copy()
{
byte[] buffer = new byte[1024 * 1024]; // 1MB buffer
bool cancelFlag = false;
using (FileStream source = new FileStream(SourceFilePath, FileMode.Open, FileAccess.Read))
{
long fileLength = source.Length;
using (FileStream dest = new FileStream(DestFilePath, FileMode.CreateNew, FileAccess.Write))
{
long totalBytes = 0;
int currentBlockSize = 0;
while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += currentBlockSize;
double persentage = (double)totalBytes * 100.0 / fileLength;
dest.Write(buffer, 0, currentBlockSize);
cancelFlag = false;
OnProgressChanged(persentage, ref cancelFlag);
if (cancelFlag == true)
{
// Delete dest file here
break;
}
}
}
}
OnComplete();
}
public string SourceFilePath { get; set; }
public string DestFilePath { get; set; }
public event ProgressChangeDelegate OnProgressChanged;
public event Completedelegate OnComplete;
}
Just run it in separate thread and subscribe for OnProgressChanged
event.
you can use Dispatcher to update your ProgressBar .
UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar1.SetValue);
Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
Here's an optimized solution that utilizes .NET extensions and a double-buffer for better performance. A new overload of CopyTo is added to FileInfo with an Action that indicates progress only when it has changed.
This sample implementation in WPF with a progress bar named progressBar1 that performs the copy operation in the background.
private FileInfo _source = new FileInfo(@"C:\file.bin");
private FileInfo _destination = new FileInfo(@"C:\file2.bin");
private void CopyFile()
{
if(_destination.Exists)
_destination.Delete();
Task.Run(()=>{
_source.CopyTo(_destination, x=>Dispatcher.Invoke(()=>progressBar1.Value = x));
}).GetAwaiter().OnCompleted(() => MessageBox.Show("File Copied!"));
}
Here's an example for a Console Application
class Program
{
static void Main(string[] args)
{
var _source = new FileInfo(@"C:\Temp\bigfile.rar");
var _destination = new FileInfo(@"C:\Temp\bigfile2.rar");
if (_destination.Exists) _destination.Delete();
_source.CopyTo(_destination, x => Console.WriteLine($"{x}% Complete"));
Console.WriteLine("File Copied.");
}
}
To use, create a new file, such as FileInfoExtensions.cs and add this code:
public static class FileInfoExtensions
{
public static void CopyTo(this FileInfo file, FileInfo destination, Action<int> progressCallback)
{
const int bufferSize = 1024 * 1024; //1MB
byte[] buffer = new byte[bufferSize], buffer2 = new byte[bufferSize];
bool swap = false;
int progress = 0, reportedProgress = 0, read = 0;
long len = file.Length;
float flen = len;
Task writer = null;
using (var source = file.OpenRead())
using (var dest = destination.OpenWrite())
{
dest.SetLength(source.Length);
for (long size = 0; size < len; size += read)
{
if ((progress = ((int)((size / flen) * 100))) != reportedProgress)
progressCallback(reportedProgress = progress);
read = source.Read(swap ? buffer : buffer2, 0, bufferSize);
writer?.Wait(); // if < .NET4 // if (writer != null) writer.Wait();
writer = dest.WriteAsync(swap ? buffer : buffer2, 0, read);
swap = !swap;
}
writer?.Wait(); //Fixed - Thanks @sam-hocevar
}
}
}
The double buffer works by using one thread to read and one thread to write, so the max speed is dictated only by the slower of the two. Two buffers are used (a double buffer), ensuring that the read and write threads are never using the same buffer at the same time.
Example: the code reads into buffer 1, then when the read completes, a write operation starts writing the contents of buffer 1. Without waiting finish writing, the buffer is swapped to buffer 2 and data is read into buffer 2 while buffer 1 is still being written. Once the read completes in buffer 2, it waits for write to complete on buffer 1, starts writing buffer 2, and the process repeats. Essentially, 1 thread is always reading, and one is always writing.
WriteAsync uses overlapped I/O, which utilizes I/O completion ports, which rely on hardware to perform asynchronous operations rather than threads, making this very efficient. TLDR: I lied about there being 2 threads, but the concept is the same.
I like this solution, because
public delegate void IntDelegate(int Int);
public static event IntDelegate FileCopyProgress;
public static void CopyFileWithProgress(string source, string destination)
{
var webClient = new WebClient();
webClient.DownloadProgressChanged += DownloadProgress;
webClient.DownloadFileAsync(new Uri(source), destination);
}
private static void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
if(FileCopyProgress != null)
FileCopyProgress(e.ProgressPercentage);
}
This should work with UNC paths as long as the permissions are set up. If not, you will get this error, in which case, I vote for the authenticated request user route.
System.UnauthorizedAccessException
: Access to the path '\testws01\c$\foo' is denied.ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via
<identity impersonate="true"/>
, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
Making your own file copy logic by using 2 streams as presented by Gal is a viable option but its not recommended solely because there is a deeply intergrated windows operation which is optimized in reliability, security and performance named CopyFileEx.
that said, in the following article: http://msdn.microsoft.com/en-us/magazine/cc163851.aspx they do exactly what you want, but ofcourse you have to use CopyFileEx
Good luck
** EDIT ** (fixed my answer, badly witten)
You can copy parts of the file stream from each file, and update after each "chunk" you update. Thus it will be more continuous - you can also easily calculate the relative size of the current "chunk" you are copying relative to the total stream size in order to show the correct percentage done.