Get size of file on disk

Viewed 94500
var length = new System.IO.FileInfo(path).Length;

This gives the logical size of the file, not the size on the disk.

I wish to get the size of a file on the disk in C# (preferably without interop) as would be reported by Windows Explorer.

It should give the correct size, including for:

  • A compressed file
  • A sparse file
  • A fragmented file
4 Answers

I think it will be like this:

double ifileLength = (finfo.Length / 1048576); //return file size in MB ....

I'm still doing some testing for this, to get a confirmation.

Related