复制过程速度

时间:2010-10-12 19:15:55

标签: c#

我认为我有一个使用system.io.File.Copy执行复制功能的进程,如何才能获得进程的复制速度?

3 个答案:

答案 0 :(得分:2)

您可以直接调用Windows API公开的CopyFileEx函数来获取进度和其他信息。

Stephen Toub在2005年2月的MSDN杂志上发表了一篇文章,其中包括一些示例代码(您可以从MSDN Magazine archive获得该文章。

以下是Stephen的文章中的代码:

public static class FileRoutines
{
    public static void CopyFile(
        FileInfo source,
        FileInfo destination,
        CopyFileOptions options = CopyFileOptions.None,
        CopyFileCallback callback = null,
        object state = null)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }

        if (destination == null)
        {
            throw new ArgumentNullException("destination");
        }

        if ((options & ~CopyFileOptions.All) != 0)
        {
            throw new ArgumentOutOfRangeException("options");
        }

        new FileIOPermission(FileIOPermissionAccess.Read, source.FullName).Demand();
        new FileIOPermission(FileIOPermissionAccess.Write, destination.FullName).Demand();

        var cpr = callback == null
            ? null
            : new CopyProgressRoutine(
               new CopyProgressData(source, destination, callback, state).CallbackHandler);

        var cancel = false;
        if (!CopyFileEx(source.FullName, destination.FullName, cpr,
            IntPtr.Zero, ref cancel, (int)options))
        {
            throw new IOException(new Win32Exception().Message);
        }
    }

    [SuppressUnmanagedCodeSecurity]
    [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool CopyFileEx(
        string lpExistingFileName, string lpNewFileName,
        CopyProgressRoutine lpProgressRoutine,
        IntPtr lpData, ref bool pbCancel, int dwCopyFlags);

    private class CopyProgressData
    {
        private readonly CopyFileCallback _callback;
        private readonly FileInfo _destination;
        private readonly FileInfo _source;
        private readonly object _state;

        public CopyProgressData(
            FileInfo source,
            FileInfo destination,
            CopyFileCallback callback,
            object state)
        {
            _source = source;
            _destination = destination;
            _callback = callback;
            _state = state;
        }

        public int CallbackHandler(
            long totalFileSize, long totalBytesTransferred,
            long streamSize, long streamBytesTransferred,
            int streamNumber, int callbackReason,
            IntPtr sourceFile, IntPtr destinationFile, IntPtr data)
        {
            return (int)_callback(_source, _destination, _state,
                totalFileSize, totalBytesTransferred);
        }
    }

    private delegate int CopyProgressRoutine(
        long totalFileSize, long TotalBytesTransferred, long streamSize,
        long streamBytesTransferred, int streamNumber, int callbackReason,
        IntPtr sourceFile, IntPtr destinationFile, IntPtr data);
}

public delegate CopyFileCallbackAction CopyFileCallback(
    FileInfo source, FileInfo destination, object state,
    long totalFileSize, long totalBytesTransferred);

public enum CopyFileCallbackAction
{
    Continue = 0,
    Cancel = 1,
    Stop = 2,
    Quiet = 3
}

[Flags]
public enum CopyFileOptions
{
    None = 0x0,
    FailIfDestinationExists = 0x1,
    Restartable = 0x2,
    AllowDecryptedDestination = 0x8,
    All = FailIfDestinationExists | Restartable | AllowDecryptedDestination
}

答案 1 :(得分:0)

复制完成后,您可以按文件的大小划分文件大小。

答案 2 :(得分:0)

您只需转到管理工具 - > 性能监视器并为正在运行的进程添加一个计数器来跟踪随时间发送的字节 - 我相信它位于.NET CLR部分下。这将实时显示文件副本的传输速率。

另外,您可以下载令人敬畏的sys内部工具Process Explorer

享受!