![](/img/trans.png)
[英]copying files from one folder to another only if their file name is a in a specified text file c#
[英]C# true time for copying files from one folder to another folder
我在 c# 中编写了一个脚本任务,我将文件从一个文件夹复制到另一个文件夹。 目标是测量复制过程从第一个文件到复制所有文件所用的时间。
我可以使用秒表可靠地测量从第一个文件到复制过程完成的真实时间吗? 或者简单地说,在我的复制方法期间代码是否继续,或者只有在整个复制过程完成并且所有文件都在新文件夹中之后?
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
//Array for all Files
string[] allfiles;
//Source of all Files in: Pfad_Quelle
string quellpfad = (string)Dts.Variables["User::Pfad_Quelle"].Value;
//Read the name of all files in the source folder and Fill the Array "allfiles"
allfiles = Directory.GetFiles(quellpfad, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();
int count = allfiles.Length;
for (int i = 1; i <= count; i++)
{
string pfad_quelle = (string)Dts.Variables["User::Pfad_Quelle"].Value + allfiles[i - 1];
string pfad_ziel = (string)Dts.Variables["User::Pfad_Ziel"].Value + allfiles[i - 1];
try
{
File.Copy(@pfad_quelle, @pfad_ziel, true);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
MessageBox.Show("RunTime " + elapsedTime);
Dts.TaskResult = (int)ScriptResults.Success;
如果要测量复制所有文件所花费的时间,请删除stopWatch.Start();
, stopWatch.Stop();
并将它们尽可能靠近File.Copy
:
...
for (int i = 1; i <= count; i++)
{
string pfad_quelle = (string)Dts.Variables["User::Pfad_Quelle"].Value + allfiles[i - 1];
string pfad_ziel = (string)Dts.Variables["User::Pfad_Ziel"].Value + allfiles[i - 1];
try
{
try
{
stopWatch.Start();
File.Copy(@pfad_quelle, @pfad_ziel, true);
}
finally
{
stopWatch.Stop();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.