开发者

Differences between 2 times?

StartCopying = DateTime.Now;
File.Copy(@"C:\Users\sshap5x\Desktop\line4\sh开发者_如何学编程iraDebbi\Hackers.avi", @"C:\Users\sshap5x\Desktop\test\Hackers.avi", true);

EndCopying = DateTime.Now;
CopyingTime1 = (endCopying - startCopying).Duration;

What is the problem with my code?

Copying time is TimeSpan object.


The assignments are to StartCopying and EndCopying but your read the data from other variables startCopying and endCopying.
C# is case sensitive.
And also Duration is a method. so you need to use .Duration()
And as suggested in the comments to your question, for better resolution use the Stopwatch class.


// This prints 0.1 (roughly)
DateTime start = DateTime.Now;
Thread.Sleep(100);
var diff = DateTime.Now - start;
Console.WriteLine(diff.TotalSeconds);

In your code (which wouldn't even compile) you use .Duration as a property when it is in fact a method. In any case, you don't need to call Duration at all because the result returned by the subtraction is a TimeSpan containing the difference. You can then get this diff in whatever format you need (milliseconds, seconds, hours, days, etc.).

The only need for the Duration method is if you are unsure whether the result is negative or positive.


Did you forget the () for Duration and capitalisation for the variables EndCopying and StartCopying?

        DateTime StartCopying = DateTime.Now;
        DateTime EndCopying = DateTime.Now;
        TimeSpan CopyingTime1 = (EndCopying - StartCopying).Duration();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜