C# 我可以在 .NET 中使用 FileInfo.CopyTo() 显示文件复制进度吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/187768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I show file copy progress using FileInfo.CopyTo() in .NET?
提问by Jason Down
I've created a copy utility in c# (.NET 2.0 Framework) that copies files, directories and recursive sub directories etc. The program has a GUI that shows the current file being copied, the current file number (sequence), the total number of files to be copied and the percentage completed for the copy operations. There is also a progress bar, that is based on current file / total files.
我在 c# (.NET 2.0 Framework) 中创建了一个复制实用程序,用于复制文件、目录和递归子目录等。该程序有一个 GUI,显示正在复制的当前文件、当前文件编号(序列)、总数要复制的文件数量和复制操作完成的百分比。还有一个进度条,它基于当前文件/总文件。
My problem is related to copying large files. I've been unable to find a way to indicate the total copy progress of a large file (using my current class structure that utilitzes FileInfo.CopyTo method). As a workaround I've separated the file copy operations and GUI display to their own threads and set up a visual cue to show that work is being done. At least the user is aware that the program isn't frozen and is still copying files.
我的问题与复制大文件有关。我一直无法找到一种方法来指示大文件的总复制进度(使用我当前使用 FileInfo.CopyTo 方法的类结构)。作为一种解决方法,我将文件复制操作和 GUI 显示分离到它们自己的线程中,并设置了一个视觉提示来显示工作正在完成。至少用户知道程序没有被冻结并且仍在复制文件。
It would be nicer to be able to show the progress based on the total number of bytes or have some type of event that fires from the FileInfo.CopyTo method that indicates the total number of bytes copied from the current file.
能够根据总字节数显示进度或具有从 FileInfo.CopyTo 方法触发的某种类型的事件会更好,该方法指示从当前文件复制的总字节数。
I'm aware of the FileInfo.Length property, so I'm sure there is a way MacGuyver my own event that is based on this and have a handler on the GUI side of things reading the updates (maybe based on checking the FileInfo.Length property of the destination object using some type of timer?).
我知道 FileInfo.Length 属性,所以我确定 MacGuyver 我自己的事件有一种基于此的方法,并且在读取更新的 GUI 端有一个处理程序(可能基于检查 FileInfo.Length 属性)。使用某种类型的计时器的目标对象的长度属性?)。
Does anyone know of a way to do this that I'm overlooking. If I can avoid it, I'd rather not rewrite my class to copy bytes through a stream and track it that way (though I'm thinking I might be stuck with going that route).
有谁知道我忽略的一种方法来做到这一点。如果我可以避免它,我宁愿不重写我的类以通过流复制字节并以这种方式跟踪它(尽管我认为我可能会坚持走那条路)。
Thanks In Advance
提前致谢
PS - I'm stuck with the .NET 2.0 framework for now, so any solution that requires features available in >= 3.0 only are not an option for me.
PS - 我现在坚持使用 .NET 2.0 框架,因此任何需要 >= 3.0 中可用功能的解决方案都不适合我。
PPS - I'm open to solutions in any .NET language variety, not only c#.
PPS - 我对任何 .NET 语言种类的解决方案持开放态度,不仅仅是 c#。
采纳答案by Gaspar Nagy
The FileInfo.CopyTo is basically a wrapper around the Win32 API call "CopyFile" in the kernel32.dll. This method does not support progress callback.
FileInfo.CopyTo 基本上是对 kernel32.dll 中 Win32 API 调用“CopyFile”的封装。此方法不支持进度回调。
However, the CopyFileEx method does, and you can write your own .NET wrapper around it in a few minutes, like it is described here: http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx
但是,CopyFileEx 方法可以,您可以在几分钟内围绕它编写自己的 .NET 包装器,就像这里描述的那样:http: //www.pinvoke.net/default.aspx/kernel32.CopyFileEx
回答by Coderer
For the love of God do not implement your own file copy using streams! The Win32 CopyFile API call that Gaspar mentioned is able to take advantage of e.g. DMA, whereas I'd bet dollars to doughnuts that the code Will wrote would not be "smart" enough to do that.
为了上帝的爱,不要使用流来实现你自己的文件复制!Gaspar 提到的 Win32 CopyFile API 调用能够利用例如 DMA,而我敢打赌,Will 编写的代码不够“聪明”,无法做到这一点。
CopyFileEx will treat you right, or you could implement a BackgroundWorker that watches the growing size of the target file and updates a progress bar using that information. The latter method saves you a PInvoke, but the former is probably a bit cleaner in the long run.
CopyFileEx 会正确对待您,或者您可以实现一个 BackgroundWorker 来监视目标文件的不断增长的大小并使用该信息更新进度条。后一种方法为您节省了 PInvoke,但从长远来看,前一种方法可能更简洁一些。
回答by cfeduke
For these sorts of things I have fallen back to Shell32 (or is it ShellUI? I don't know anymore). This gives you a native Windows dialog that users are used to seeing for copying operations. I guess it would replace your already existing dialog so it may not be the right answer for you, but it is useful to remember for those "in a pinch" scenarios.
对于这些事情,我已经退回到 Shell32(或者是 ShellUI?我不知道了)。这为您提供了一个本地 Windows 对话框,用户习惯于在进行复制操作时看到该对话框。我想它会取代你已经存在的对话框,所以它可能不是你的正确答案,但对于那些“紧要关头”的场景来说,记住它是有用的。
Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(
srcPath,
dstPath,
Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException
);
Yes, you must reference the Microsoft.VisualBasic assembly. I've grown to lovethis assembly.
是的,您必须引用 Microsoft.VisualBasic 程序集。我越来越喜欢这个大会。
回答by Dennis
I also used the implementation provided in the marked answer. However I then created a wrapper to provide a nicer? API to use from .NET.
我还使用了标记答案中提供的实现。但是我然后创建了一个包装器来提供更好的?从 .NET 使用的 API。
Usage:
用法:
XCopy.Copy(networkFile.FullPath, temporaryFilename, true, true, (o, pce) =>
{
worker.ReportProgress(pce.ProgressPercentage, networkFile);
});
Implementation
执行
/// <summary>
/// PInvoke wrapper for CopyEx
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852.aspx
/// </summary>
public class XCopy
{
public static void Copy(string source, string destination, bool overwrite, bool nobuffering)
{
new XCopy().CopyInternal(source, destination, overwrite, nobuffering, null);
}
public static void Copy(string source, string destination, bool overwrite, bool nobuffering, EventHandler<ProgressChangedEventArgs> handler)
{
new XCopy().CopyInternal(source, destination, overwrite, nobuffering, handler);
}
private event EventHandler Completed;
private event EventHandler<ProgressChangedEventArgs> ProgressChanged;
private int IsCancelled;
private int FilePercentCompleted;
private string Source;
private string Destination;
private XCopy()
{
IsCancelled = 0;
}
private void CopyInternal(string source, string destination, bool overwrite, bool nobuffering, EventHandler<ProgressChangedEventArgs> handler)
{
try
{
CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE;
if (!overwrite)
copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;
if (nobuffering)
copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING;
Source = source;
Destination = destination;
if (handler != null)
ProgressChanged += handler;
bool result = CopyFileEx(Source, Destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags);
if (!result)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
catch (Exception)
{
if (handler != null)
ProgressChanged -= handler;
throw;
}
}
private void OnProgressChanged(double percent)
{
// only raise an event when progress has changed
if ((int)percent > FilePercentCompleted)
{
FilePercentCompleted = (int)percent;
var handler = ProgressChanged;
if (handler != null)
handler(this, new ProgressChangedEventArgs((int)FilePercentCompleted, null));
}
}
private void OnCompleted()
{
var handler = Completed;
if (handler != null)
handler(this, EventArgs.Empty);
}
#region PInvoke
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);
private delegate CopyProgressResult CopyProgressRoutine(long TotalFileSize, long TotalBytesTransferred, long StreamSize, long StreamBytesTransferred, uint dwStreamNumber, CopyProgressCallbackReason dwCallbackReason,
IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData);
private enum CopyProgressResult : uint
{
PROGRESS_CONTINUE = 0,
PROGRESS_CANCEL = 1,
PROGRESS_STOP = 2,
PROGRESS_QUIET = 3
}
private enum CopyProgressCallbackReason : uint
{
CALLBACK_CHUNK_FINISHED = 0x00000000,
CALLBACK_STREAM_SWITCH = 0x00000001
}
[Flags]
private enum CopyFileFlags : uint
{
COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
COPY_FILE_NO_BUFFERING = 0x00001000,
COPY_FILE_RESTARTABLE = 0x00000002,
COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008
}
private CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long streamByteTrans, uint dwStreamNumber,
CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
{
if (reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED)
OnProgressChanged((transferred / (double)total) * 100.0);
if (transferred >= total)
OnCompleted();
return CopyProgressResult.PROGRESS_CONTINUE;
}
#endregion
}
回答by srsyogesh
Thanks to @Gasper and @Dennis for pointing out CopyFileEx method. I have extended dennis answer with abort copy
感谢@Gasper 和@Dennis 指出 CopyFileEx 方法。我用中止复制扩展了丹尼斯的回答
/// <summary>
/// Type indicates how the copy gets completed.
/// </summary>
internal enum CopyCompletedType
{
Succeeded,
Aborted,
Exception
}
/// <summary>
/// Event arguments for file copy
/// </summary>
internal class FileCopyEventArgs : EventArgs
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="type">type of the copy completed type enum</param>
/// <param name="exception">exception if any</param>
public FileCopyEventArgs(CopyCompletedType type, Exception exception)
{
Type = type;
Exception = exception;
}
/// <summary>
/// Type of the copy completed type
/// </summary>
public CopyCompletedType Type
{
get;
private set;
}
/// <summary>
/// Exception if any happend during copy.
/// </summary>
public Exception Exception
{
get;
private set;
}
}
/// <summary>
/// PInvoke wrapper for CopyEx
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852.aspx
/// </summary>
internal class XCopy
{
private int IsCancelled;
private int FilePercentCompleted;
public XCopy()
{
IsCancelled = 0;
}
/// <summary>
/// Copies the file asynchronously
/// </summary>
/// <param name="source">the source path</param>
/// <param name="destination">the destination path</param>
/// <param name="nobuffering">Bufferig status</param>
/// <param name="handler">Event handler to do file copy.</param>
public void CopyAsync(string source, string destination, bool nobuffering)
{
try
{
//since we needed an async copy ..
Action action = new Action(
() => CopyInternal(source, destination, nobuffering)
);
Task task = new Task(action);
task.Start();
}
catch (AggregateException ex)
{
//handle the inner exception since exception thrown from task are wrapped in
//aggreate exception.
OnCompleted(CopyCompletedType.Exception, ex.InnerException);
}
catch (Exception ex)
{
OnCompleted(CopyCompletedType.Exception, ex);
}
}
/// <summary>
/// Event which will notify the subscribers if the copy gets completed
/// There are three scenarios in which completed event will be thrown when
/// 1.Copy succeeded
/// 2.Copy aborted.
/// 3.Any exception occured.
/// These information can be obtained from the Event args.
/// </summary>
public event EventHandler<FileCopyEventArgs> Completed;
/// <summary>
/// Event which will notify the subscribers if there is any progress change while copying.
/// This will indicate the progress percentage in its event args.
/// </summary>
public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
/// <summary>
/// Aborts the copy asynchronously and throws Completed event when done.
/// User may not want to wait for completed event in case of Abort since
/// the event will tell that copy has been aborted.
/// </summary>
public void AbortCopyAsync()
{
Trace.WriteLine("Aborting the copy");
//setting this will cancel an operation since we pass the
//reference to copyfileex and it will periodically check for this.
//otherwise also We can check for iscancelled on onprogresschanged and return
//Progress_cancelled .
IsCancelled = 1;
Action completedEvent = new Action(() =>
{
//wait for some time because we ll not know when IsCancelled is set , at what time windows stops copying.
//so after sometime this may become valid .
Thread.Sleep(500);
//do we need to wait for some time and send completed event.
OnCompleted(CopyCompletedType.Aborted);
//reset the value , otherwise if we try to copy again since value is 1 ,
//it thinks that its aborted and wont allow to copy.
IsCancelled = 0;
});
Task completedTask = new Task(completedEvent);
completedTask.Start();
}
/// <summary>
/// Copies the file using asynchronos task
/// </summary>
/// <param name="source">the source path</param>
/// <param name="destination">the destination path</param>
/// <param name="nobuffering">Buffering status</param>
/// <param name="handler">Delegate to handle Progress changed</param>
private void CopyInternal(string source, string destination, bool nobuffering)
{
CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE;
if (nobuffering)
{
copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING;
}
try
{
Trace.WriteLine("File copy started with Source: " + source + " and destination: " + destination);
//call win32 api.
bool result = CopyFileEx(source, destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags);
if (!result)
{
//when ever we get the result as false it means some error occured so get the last win 32 error.
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
catch (Exception ex)
{
//the mesage will contain the requested operation was aborted when the file copy
//was cancelled. so we explicitly check for that and do a graceful exit
if (ex.Message.Contains("aborted"))
{
Trace.WriteLine("Copy aborted.");
}
else
{
OnCompleted(CopyCompletedType.Exception, ex.InnerException);
}
}
}
private void OnProgressChanged(double percent)
{
// only raise an event when progress has changed
if ((int)percent > FilePercentCompleted)
{
FilePercentCompleted = (int)percent;
var handler = ProgressChanged;
if (handler != null)
{
handler(this, new ProgressChangedEventArgs((int)FilePercentCompleted, null));
}
}
}
private void OnCompleted(CopyCompletedType type, Exception exception = null)
{
var handler = Completed;
if (handler != null)
{
handler(this, new FileCopyEventArgs(type, exception));
}
}
#region PInvoke
/// <summary>
/// Delegate which will be called by Win32 API for progress change
/// </summary>
/// <param name="total">the total size</param>
/// <param name="transferred">the transferrred size</param>
/// <param name="streamSize">size of the stream</param>
/// <param name="streamByteTrans"></param>
/// <param name="dwStreamNumber">stream number</param>
/// <param name="reason">reason for callback</param>
/// <param name="hSourceFile">the source file handle</param>
/// <param name="hDestinationFile">the destination file handle</param>
/// <param name="lpData">data passed by users</param>
/// <returns>indicating whether to continue or do somthing else.</returns>
private CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long streamByteTrans, uint dwStreamNumber,
CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
{
//when a chunk is finished call the progress changed.
if (reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED)
{
OnProgressChanged((transferred / (double)total) * 100.0);
}
//transfer completed
if (transferred >= total)
{
if (CloseHandle(hDestinationFile))
{
OnCompleted(CopyCompletedType.Succeeded, null);
}
else
{
OnCompleted(CopyCompletedType.Exception,
new System.IO.IOException("Unable to close the file handle"));
}
}
return CopyProgressResult.PROGRESS_CONTINUE;
}
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);
private delegate CopyProgressResult CopyProgressRoutine(long TotalFileSize, long TotalBytesTransferred, long StreamSize, long StreamBytesTransferred, uint dwStreamNumber, CopyProgressCallbackReason dwCallbackReason,
IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData);
private enum CopyProgressResult : uint
{
PROGRESS_CONTINUE = 0,
PROGRESS_CANCEL = 1,
PROGRESS_STOP = 2,
PROGRESS_QUIET = 3
}
private enum CopyProgressCallbackReason : uint
{
CALLBACK_CHUNK_FINISHED = 0x00000000,
CALLBACK_STREAM_SWITCH = 0x00000001
}
[Flags]
private enum CopyFileFlags : uint
{
COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
COPY_FILE_NO_BUFFERING = 0x00001000,
COPY_FILE_RESTARTABLE = 0x00000002,
COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008
}
#endregion
}
The clients can create an object of XCopy class and call copy/abort method .
客户端可以创建 XCopy 类的对象并调用 copy/abort 方法。
回答by AJ Richardson
I know I'm a bit late to the party, but I made a wrapper for CopyFileEx
that returns a Task
and accepts a CancellationToken
and IProgress<double>
. Unfortunately it won't work in the .NET 2.0 framework, but for anyone using 4.5, this allows you to use the await
keyword.
我知道我参加聚会有点晚了,但是我为此做了一个包装器CopyFileEx
,返回 aTask
并接受 a CancellationToken
and IProgress<double>
。不幸的是,它在 .NET 2.0 框架中不起作用,但对于使用 4.5 的任何人,这允许您使用await
关键字。
public static class FileEx
{
public static Task CopyAsync(string sourceFileName, string destFileName)
{
return CopyAsync(sourceFileName, destFileName, CancellationToken.None);
}
public static Task CopyAsync(string sourceFileName, string destFileName, CancellationToken token)
{
return CopyAsync(sourceFileName, destFileName, token, null);
}
public static Task CopyAsync(string sourceFileName, string destFileName, IProgress<double> progress)
{
return CopyAsync(sourceFileName, destFileName, CancellationToken.None, progress);
}
public static Task CopyAsync(string sourceFileName, string destFileName, CancellationToken token, IProgress<double> progress)
{
int pbCancel = 0;
CopyProgressRoutine copyProgressHandler;
if (progress != null)
{
copyProgressHandler = (total, transferred, streamSize, streamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) =>
{
progress.Report((double)transferred / total * 100);
return CopyProgressResult.PROGRESS_CONTINUE;
};
}
else
{
copyProgressHandler = EmptyCopyProgressHandler;
}
token.ThrowIfCancellationRequested();
var ctr = token.Register(() => pbCancel = 1);
var copyTask = Task.Run(() =>
{
try
{
CopyFileEx(sourceFileName, destFileName, copyProgressHandler, IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE);
token.ThrowIfCancellationRequested();
}
finally
{
ctr.Dispose();
}
}, token);
return copyTask;
}
private static CopyProgressResult EmptyCopyProgressHandler(long total, long transferred, long streamSize, long streamByteTrans, uint dwStreamNumber, CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
{
return CopyProgressResult.PROGRESS_CONTINUE;
}
#region DLL Import
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName,
CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel,
CopyFileFlags dwCopyFlags);
delegate CopyProgressResult CopyProgressRoutine(
long totalFileSize,
long totalBytesTransferred,
long streamSize,
long streamBytesTransferred,
uint dwStreamNumber,
CopyProgressCallbackReason dwCallbackReason,
IntPtr hSourceFile,
IntPtr hDestinationFile,
IntPtr lpData);
enum CopyProgressResult : uint
{
PROGRESS_CONTINUE = 0,
PROGRESS_CANCEL = 1,
PROGRESS_STOP = 2,
PROGRESS_QUIET = 3
}
enum CopyProgressCallbackReason : uint
{
CALLBACK_CHUNK_FINISHED = 0x00000000,
CALLBACK_STREAM_SWITCH = 0x00000001
}
[Flags]
enum CopyFileFlags : uint
{
COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
COPY_FILE_RESTARTABLE = 0x00000002,
COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008
}
#endregion
}
回答by Martin Ch
If someone still stumbles into this problem (10 years later!) as I did, I have created a wrapper around CopyFileEx and MoveFileWithProgress functions (as some of the answers here) with some extra, useful functionality (such as async, access rights check, formatting of bytes, copying directories...)
如果有人仍然像我一样偶然发现这个问题(10 年后!),我已经创建了一个围绕 CopyFileEx 和 MoveFileWithProgress 函数(作为这里的一些答案)的包装器,其中包含一些额外的、有用的功能(例如异步、访问权限检查、字节格式,复制目录...)