2010-10-12 47 views
0

認爲我有一個進程與system.io.File.Copy執行復制功能,我怎樣才能獲得我的進程的複製速度?複製速度的過程

回答

0

複製完成後,您可以將文件的大小除以所需的時間。

+1

如果他們需要知道它*因爲文件正在被複制,這並沒有什麼幫助。 – LittleBobbyTables 2010-10-12 19:22:50

2

您可以通過直接調用Windows API公開的CopyFileEx函數獲得進度和其他信息。

斯蒂芬Toub曾在MSDN雜誌2005年2月刊,包括一些示例代碼的文章(你可以從MSDN Magazine archive文章

這裏是斯蒂芬的文章的代碼:

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 
} 
+0

該鏈接僅指向MSDN雜誌存檔 - 請參閱MSDN雜誌2005年2月.Net重要事項圖1 FileRoutines.CopyFile使用Win32 CopyFileEx – 2015-09-18 17:09:28

0

您可以簡單地轉到管理工具 - >性能監視器,併爲您的運行進程添加一個計數器,以跟蹤隨時間發送的字節數 - 我相信它在.NET CLR部分下,這將實時顯示您的傳輸速率您的文件副本。

Addtionally你可以下載令人敬畏的sys內部工具Process Explorer

享受!