5

How to read the logical and physical file size using C# api.

enter image description here

Anil Namde
  • 6,452
  • 11
  • 63
  • 100
  • 2
    possible duplicate of http://stackoverflow.com/questions/3750590/c-get-file-size-on-disk – Christian May 11 '11 at 06:16
  • @Christian - Not a duplicate. This question is asking about the logical size as well as the physical. – Oded May 11 '11 at 06:17
  • 1
    @Oded - The guy asking the question in the link already provides a method of finding the logical size: var length = new System.IO.FileInfo(path).Length; – Christian May 11 '11 at 06:18
  • @Christian - The answer does, the question however, is not the same. You could have linked to the answer as one that answer this question as well. – Oded May 11 '11 at 06:20
  • @Oded - okay thats true. But all information can be found in the link, including complete code for both methods. – Christian May 11 '11 at 06:23

1 Answers1

2
(new FileInfo(path).Length)

is the actual size. As for size on disk, I don't think there's an API to get it, but you can get it using the actual size, and the cluster size.

There's some info on the calculation required here: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/85bf76ac-a254-41d4-a3d7-e7803c8d9bc3

Gal
  • 5,338
  • 5
  • 33
  • 55
  • This isn't always correct... eg some files processed by archiving systems have a size on disk = a single cluster (say 4k), no matter the size reported - See eg Symantex Enterprise Vault – Basic Nov 01 '13 at 22:16