开发者

Copy Speed of Process

think I have a process that perform Copy function with system.io.File开发者_Go百科.Copy , how can I get copy speed of my process ?


You can get progress and other information by directly calling the CopyFileEx function exposed by the Windows API.

Stephen Toub had an article in the 2005 February issue of MSDN Magazine including some sample code (You can get the article from the MSDN Magazine archive.

Here is the code from Stephen's article:

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
}


After copy has finished, you can divide the size of the file(s) by the time taken.


You can simply go to Administrative Tools -> Performance Monitor and add a counter for your running process to track the bytes sent over time - I believe it is under the .NET CLR Section. This will show you in real time the transfer rate of your file copy.

Addtionally you can download the awesome sys internal tool Process Explorer.

Enjoy!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜