Dot-Net

My.Computer.FileSystem 和 System.IO.File 之間到底有什麼區別

  • August 16, 2021

My.Computer.FileSystem和命名空間中有很多重複的功能System.IO.File

那麼究竟有什麼區別:

My.Computer.FileSystem.CopyFile(source, dest, True)

和:

System.IO.File.Copy(source, dest, True)

有性能差異嗎?大家對哪個在閱讀能力上有優勢有什麼看法?我個人使用My.Computer命名空間,但這只是現在的習慣。

My.* 只是為 VB.NET 實現的一組外觀模式類,其中包含常見的 System.IO*(和其他)操作。由於您正在經歷一個額外的抽象層,因此對性能的影響非常小,但您必須決定是否值得為此進行優化。我建議使用對您和您商店中的其他人有意義的任何方式。

如果您使用 .NET Reflector 檢查程式碼,My.Computer.FileSystem.CopyFile您會發現該方法包裝了許多 System.IO 類,例如 File 和 Directory,尤其是 File 類的 Copy、Move 和 Delete 方法。片段:

'lots of other code snipped out for brevity and to show the use of System.IO classes...

Directory.CreateDirectory(FileSystem.GetParentPath(str))

  'snip

   If 
      ' snip
   Else
       File.Delete(str)
       File.Move(path, str)
   End If
Else
   File.Move(path, str)
End If
End Sub

引用自:https://stackoverflow.com/questions/5871214