正如標題所示,以下哪種情況會更快?FileInfo.Exists/Copy比File快嗎?
// Using FileInfo
FileInfo file = new FileInfo(@"C:\Test.txt");
if (file.Exists)
file.CopyTo(@"C:\TestCopy.txt");
// Using File
if (File.Exists(@"C:\Test.txt"))
File.Copy(@"C:\Test.txt", @"C:\TestCopy.txt");
我知道FileInfo更易於閱讀,但其中一種方法比另一種更快嗎?
您是否試圖爲您的案例編寫基準? – Romoku
寫小測試並分享結果 – Mzf
請參閱:http://stackoverflow.com/questions/1324788/what-is-the-difference-between-file-and-fileinfo-in-c。 YMMV,並且將取決於你的具體用例,因此按其他人的建議進行基準測試可能是找出問題的最佳方法。 –