Introduction
After reading about lots of ways to copy files with a progress indicator in C# and finding them all to be a little... long winded, I decided to write this article to help my fellow coders. It's a simple way to copy files and folders with a customizable progress bar/indicator.
Background
The class uses the Windows Kernal32 CopyFileEx
function to do the copying of files. This being said, it does use one pointer, and so needs to be compiled with the /unsafe parameter (or simply tick the 'allow unsafe code' option in the project's properties)
You can create your own way to display the progress of the copy. To do this, simply implement the ICopyFileDiag
interface and pass your class to the copy method.
public interface ICopyFilesDiag
{
System.ComponentModel.ISynchronizeInvoke SynchronizationObject { get; set; }
event CopyFiles.DEL_cancelCopy EN_cancelCopy;
void update(Int32 totalFiles, Int32 copiedFiles, Int64 totalBytes,
Int64 copiedBytes, String currentFilename);
void Show();
void Hide();
}
Using the code
The guts of the code is in the CopyFiles class; however, once this is referenced in your application, it's simply a case of specifying a list of files for a directory to copy and the location to copy it to.
You then choose to do the copy asynchronously or synchronously:
private void But_CopyFiles_Click(object sender, EventArgs e)
{
List<String> TempFiles = new List<String>();
TempFiles.Add("C:\\Copy Test Folder\\test.txt");
TempFiles.Add("C:\\Copy Test Folder\\Bob.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test4\\Bob.Trev.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test4\\test.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test3\\Bob.Trev.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test3\\test.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test3\\B.o.b.Trev..txt");
TempFiles.Add("C:\\Copy Test Folder\\Test2\\Bob.Trev.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test2\\test.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test1\\test.txt");
TempFiles.Add("C:\\Copy Test Folder\\Test1\\B.o.b.Trev..txt");
TempFiles.Add("C:\\Copy Test Folder\\Test1\\Bob.Trev.txt");
CopyFiles.CopyFiles Temp = new CopyFiles.CopyFiles(TempFiles, "C:\\Test");
CopyFiles.DIA_CopyFiles TempDiag = new CopyFiles.DIA_CopyFiles();
TempDiag.SynchronizationObject = this;
Temp.CopyAsync(TempDiag);
}
This is an example of how the ICopyFileDiag
can be implemented:
public partial class DIA_CopyFiles : Form, ICopyFilesDiag
{
public System.ComponentModel.ISynchronizeInvoke
SynchronizationObject { get; set; }
public DIA_CopyFiles()
{
InitializeComponent();
}
public void update(Int32 totalFiles, Int32 copiedFiles,
Int64 totalBytes, Int64 copiedBytes, String currentFilename)
{
Prog_TotalFiles.Maximum = totalFiles;
Prog_TotalFiles.Value = copiedFiles;
Prog_CurrentFile.Maximum = 100;
if (totalBytes != 0)
{
Prog_CurrentFile.Value = Convert.ToInt32((100f /
(totalBytes / 1024f)) * (copiedBytes / 1024f));
}
Lab_TotalFiles.Text = "Total files (" + copiedFiles +
"/" + totalFiles + ")";
Lab_CurrentFile.Text = currentFilename;
}
private void But_Cancel_Click(object sender, EventArgs e)
{
RaiseCancel();
}
private void DIA_CopyFiles_Closed(object sender, System.EventArgs e)
{
RaiseCancel();
}
private void RaiseCancel()
{
if (EN_cancelCopy != null)
{
EN_cancelCopy();
}
}
public event CopyFiles.DEL_cancelCopy EN_cancelCopy;
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.