6
How I use the C function# File.Copy(string path);
to copy a file by incrementing a value in a Progressbar?
6
How I use the C function# File.Copy(string path);
to copy a file by incrementing a value in a Progressbar?
4
This function cannot be used to show progress (it basically calls the native function - Win32 API - CopyFile
kernel32.dll). There is no function in the class File
make the copy with progress notification, but you can create a wrapper
(using Pinvoke) on the native function CopyFileEx
from the same library, as described here: http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx.
The answer to a similar question in the English OS has a complete wrapper implementation.
3
I found a simpler implementation in the response from Anton Semenov.
public delegate void ProgressChangeDelegate(double percentage, ref bool cancel);
public delegate void CompleteDelegate();
class CustomFileCopier {
public CustomFileCopier(string source, string dest) {
this.SourceFilePath = source;
this.DestFilePath = dest;
OnProgressChanged += delegate { }; //só para facilitar o exemplo aqui
OnComplete += delegate { }; //só para facilitar o exemplo aqui
}
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 percentage = (double)totalBytes * 100.0 / fileLength;
dest.Write(buffer, 0, currentBlockSize);
cancelFlag = false;
OnProgressChanged(percentage, ref cancelFlag);
if (cancelFlag == true) {break; } // Delete dest file here
}
}
}
OnComplete();
}
public string SourceFilePath { get; set; }
public string DestFilePath { get; set; }
public event ProgressChangeDelegate OnProgressChanged;
public event CompleteDelegate OnComplete;
}
I put in the Github for future reference.
You can make it more complete. You will have to implement the event subscription, something like:
(percentage, cancelFlag) => { if (OnProgressChanged != null)
OnProgressChanged(percentage, ref cancelFlag); };
and
(percentage, cancelFlag) => { if (OnComplete != null)
OnComplete(percentage, ref cancelFlag); };
You need to rotate in another thread and sign the event OnProgressChanged
to receive the copy progress notifications.
If you are using Winforms and want a progress bar ready the . NET has one. I don’t know if it would be compatible with the above class. I didn’t have time to test but I don’t see why not. You would run the PerformStep()
within the method that signs the event OnProgressChanged
.
Some other options (doesn’t mean I find them good or bad):
1
Adds reference to library: Microsoft.VisualBasic.dll
(A DLL can be found in the folder C: Windows Microsoft.NET Framework.net.xxx) Choose version . NET according to your project
In the code:
using Microsoft.VisualBasic.FileIO;
...
{
FileSystem.CopyFile(ArqOrigem, ArqDestino, UIOption.AllDialogs);
}
...
Source: https://stackoverflow.com/questions/6687443/how-to-bring-up-the-built-in-file-copy-dialog
Yes but I am using C# not VB
And yet all you have is this: http://prntscr.com/9ko87h
This code is C#, add reference to DLL (Microsoft.VisualBasic.dll), search the folder: ; C:\Windows\Microsoft.NET\Framework\versão.net.xxx
ex: C:\Windows\Microsoft.NET\Framework\v2.0.50727
Browser other questions tagged c# .net winforms filing-cabinet
You are not signed in. Login or sign up in order to post.
Nathan, the HTML support on the site is very basic, prefer to use Markdown: http://answall.com/editing-help
– brasofilo