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

问题描述 投票:0回答:1

我是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();
}
c# performance file copy file-transfer
1个回答
0
投票

您可以像这样启动秒表。

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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.