3

Why there are two class for the almost same purpose System.IO.File and System.IO.FileInfo.

Jeevan Bhatt
  • 5,881
  • 18
  • 54
  • 82

2 Answers2

5

System.IO.File provides static members related to working with files, whereas System.IO.FileInfo represents a specific file and contains non-static members for working with that file.

From MSDN:

Because all File methods are static, it might be more efficient to use a File method rather than a corresponding FileInfo instance method if you want to perform only one action. All File methods require the path to the file that you are manipulating.

The static methods of the File class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of FileInfo instead, because the security check will not always be necessary.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
5

In general they do two very different things:

  • System.IO.File - getting/working with any file
  • System.IO.FileInto - getting/working with (including metadata) a particular file

Granted they do share a few methods with the same purpose, but for the most part they have very different purposes/scenarios that they serve best.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155