How to get the size on disk of a file in c#
-
Tuesday, October 24, 2006 1:09 PM
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
All Replies
-
Tuesday, October 24, 2006 1:32 PM
-
Tuesday, October 24, 2006 2:23 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:52 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 3:17 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
-
Saturday, October 28, 2006 7:46 PMModerator
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. -
Thursday, November 02, 2006 9:15 AM
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
:-) -
Friday, March 02, 2007 4:01 AMThe Win32 function GetCompressedFileSize() can get the size of a compressed file. Use File.GetAttributes() to determine whether the file is compressed.
-
Sunday, March 27, 2011 7:11 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.
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
-
Monday, September 05, 2011 12:50 AM
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.

