I wrote a script Task in c# where I copy files from one folder into another. The goal is to measure how long the copying process is taking from the first file until all files are copied.
Can I measure the true time, from the first file until the copying process is completed, reliably by using the Stopwatch? Or simply, does the code continue during my copying method, or only after the whole copying process is competed and all files are in the new folder?
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;
If you want to measure time spent to copy all the files, remove
stopWatch.Start();,stopWatch.Stop();and put them as close toFile.Copyas you can: