在 C# 中复制文件后,如何捕获 File.Copy 的传输时间?

How can I catch the transfer time of a File.Copy after copying files in C#?

我是 C# 的初学者,我不得不用一个小的 C# 控制台应用程序将文件从一个目录复制到另一个目录,我在整个网络上搜索了一个提示,以便在 File.Copy 之后捕捉传输时间,但我没有找到,有人能告诉我是否有方法或者我必须从头开始创建它谢谢你指导我

public void Copy(string path, string srcPath)
{
    var diSource = new DirectoryInfo(srcPath);
    var diTarget = new DirectoryInfo(path);

    CopyAll(diSource, diTarget);
}

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
    Directory.CreateDirectory(target.FullName);

    // Copy each file into the new directory.
    foreach (FileInfo fi in source.GetFiles())
    {
        Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
        fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
    }

    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir =
        target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

static void Main(string[] args)
{
    string UID = Console.ReadLine();

    DateTime now = DateTime.Now;
    string dateT = now.ToString("dd MMMM yyyy hh:mm:ss tt");

    string path = @"C:\Users\ASUS\Desktop\Backup";

    string task = Console.ReadLine();

    string srcPathDir = Console.ReadLine();
    string srcPath = @"C:\Users\ASUS\Desktop\"+srcPathDir;

    StoreLogs Logs = new StoreLogs();

    // Copy files from the source directory to the destination
    Logs.Copy(path, srcPath);

    Console.ReadLine();
}

你可以像这样启动秒表。

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
 static void Main(string[] args)
 {
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    Thread.Sleep(10000);
    stopWatch.Stop();
    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value.
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
    Console.WriteLine("RunTime " + elapsedTime);
  }
}