我正在使用秒表,我想估計從開始到結束需要多長時間。它看起來像一個非常簡單的復合體。我實例化了秒表,啟動它,然后停止它,然后通過編寫一個方法 .elapsed 它應該為我提供從開始到停止所需的時間。不幸的是,它總是為我提供 00:00:00。我是否必須在我的電腦上設置一些適合我的文化的內容?我也嘗試過 .StartNew() 但無濟于事。 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); string juniorDevOpsFolder = "JuniorDevOps"; string targetPath = Directory.GetCurrentDirectory(); int endIndex = targetPath.IndexOf(juniorDevOpsFolder); var juniorDevOpsPath = targetPath.Substring(0, endIndex + juniorDevOpsFolder.Length); string directory = "Files"; string targetDirectory = Path.Combine(juniorDevOpsPath, directory); ProcessDirectory(targetDirectory); stopwatch.Stop(); // Write hours, minutes and seconds. Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed);同樣,輸出為 00:00:00
4 回答

SMILET
TA貢獻1796條經驗 獲得超4個贊
您的程序執行時間不到一秒,因此格式化stopwatch.Elapsed為將 00:00:00 打印到控制臺。嘗試stopwatch.ElapsedMilliseconds而不是格式化stopwatch.Elapsed。
就像是
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//Your business logic
stopwatch.Stop();
Console.WriteLine($"Elapsed milliseconds : {stopwatch.ElapsedMilliseconds}" );

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
那段代碼呢:
var watch = new System.Diagnostics.Stopwatch(); watch.Start(); // do something what do you want watch.Stop(); Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
- 4 回答
- 0 關注
- 200 瀏覽
添加回答
舉報
0/150
提交
取消