locked
How to get the size on disk of a file in c#

    Question

  • Hi

         I want to get the size on disk of a selected file in c#.By using FileInfo.Length i am able to get the file size.But i need "size on disk" in c#.

                        Thanks in advance

    Thanks

    Chandu.Sanka

    Tuesday, October 24, 2006 1:09 PM

Answers

  • With the DriveInfo class.

    Regards.
    Tuesday, October 24, 2006 1:32 PM
  • The size on disk should be the sum of the size of the clusters that store the file:
      long sizeondisk = clustersize * ((filelength + clustersize - 1) / clustersize);

    You'll need to dip into P/Invoke to find the cluster size; GetDiskFreeSpace() returns it.
    Saturday, October 28, 2006 7:46 PM
    Moderator

All replies

  • With the DriveInfo class.

    Regards.
    Tuesday, October 24, 2006 1:32 PM
  • HI  V.Tortola

          when i click  file properties window.below the size property of the file,there is another property as "SIZE ON DISK".I need the value of that property.I don't need hard disk information.

    • Proposed as answer by KraGiE Tuesday, September 13, 2011 11:53 PM
    Tuesday, October 24, 2006 2:23 PM
  • The size on disk is returned by the FileInfo object or the StreamReader base object length property- it will get the value on how many bytes are available to read for that file as its true value.

    I'm curious why you need the other property? (Size on disk)

    Tuesday, October 24, 2006 2:52 PM
  • Hi ahmedilyas

       i want to display properties form when i click properties of a file from my application itself.Right now i am able to retrive all the information regarding a file like file type,creation,modified time etc..,There is only one task left.that is "SIZE ON DISK".

    Thanks

    Chandu.Sanka

    Tuesday, October 24, 2006 3:17 PM
  • The size on disk should be the sum of the size of the clusters that store the file:
      long sizeondisk = clustersize * ((filelength + clustersize - 1) / clustersize);

    You'll need to dip into P/Invoke to find the cluster size; GetDiskFreeSpace() returns it.
    Saturday, October 28, 2006 7:46 PM
    Moderator
  • If you are using ntfs this approach won't work for files with the compressed attribute on.
    On such files the 'size on disk' is usually smaller than the actual file size 
       :-)

     

     

    Thursday, November 02, 2006 9:15 AM
  • The Win32 function GetCompressedFileSize() can get the size of a compressed file. Use File.GetAttributes() to determine whether the file is compressed.
    • Proposed as answer by heinzdmx Tuesday, March 13, 2012 6:54 PM
    • Unproposed as answer by heinzdmx Tuesday, March 13, 2012 6:54 PM
    Friday, March 02, 2007 4:01 AM
  • The size on disk should be the sum of the size of the clusters that store the file:
      long sizeondisk = clustersize * ((filelength + clustersize - 1) / clustersize);

    You'll need to dip into P/Invoke to find the cluster size; GetDiskFreeSpace() returns it.


    This formula is realy stupid and useless because it means that:

    clustersize * ((filelength + clustersize - 1) / clustersize)=

    = (clustersize /clustersize)* (filelength + clustersize - 1)=

    = 1* (filelength + clustersize - 1)=

    =filelength + clustersize - 1

    and this is not true

    Sunday, March 27, 2011 7:11 PM
  • You're forgetting that this is a computer program, not a purely mathematical equation.
    ((filelength + clustersize - 1) / clustersize) is calculated independently and since it's an operation on integers, the result is floored to the closest lower integer.

    Therefore what you are saying is incorrect.

    Monday, September 05, 2011 12:50 AM