Size and Size on Disk information.

  • Thread starter Thread starter Suresh.Eddala
  • Start date Start date
S

Suresh.Eddala

Hi,
I am trying to get file size information in my code.

FileInfo().Length is returning the size of the file.

I have checked on File information in windows right click properties,
it has two properties.
-- Size
-- Size on disk

It seems size on disk is vary based on file system type and cluster
size,
Can any one tell me. How we can get Size on disk value for file.


Thank you!
Suresh
 
Hi,
I am trying to get file size information in my code.

FileInfo().Length is returning the size of the file.
[snip]
Can any one tell me. How we can get Size on disk value for file.


Thank you!
Suresh

Suresh,

Use Google to search for how to get the cluster size of a drive.

Once you have the cluster size, the size on disk would be:

long fileSize = new FileInfo("file").Length;
int clusterSize = ????;
long fileSizeOnDisk = fileSize % clusterSize == 0 ? fileSize : fileSize
+ (clusterSize - fileSize % clusterSize);

You just have to "round up" to the nearest cluster.


Dan Manges
 
Not always true if you are dealing with sparse files though. The file
size could be 2gb, but only use 1gb on disk due to large chunks of it
being zeros.


Hi,
I am trying to get file size information in my code.

FileInfo().Length is returning the size of the file.
[snip]
Can any one tell me. How we can get Size on disk value for file.


Thank you!
Suresh

Suresh,

Use Google to search for how to get the cluster size of a drive.

Once you have the cluster size, the size on disk would be:

long fileSize = new FileInfo("file").Length;
int clusterSize = ????;
long fileSizeOnDisk = fileSize % clusterSize == 0 ? fileSize : fileSize
+ (clusterSize - fileSize % clusterSize);

You just have to "round up" to the nearest cluster.


Dan Manges
 
Chris said:
Not always true if you are dealing with sparse files though. The file
size could be 2gb, but only use 1gb on disk due to large chunks of it
being zeros.

Only if you have some sort of hard drive compression enabled.

I just made a 1GB file of all 0's like this:

long bytes = 1024 * 1024 * 1024;
string file = @"c:\zero-data.tmp";
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(file)))
{
for (long i = 0; i < bytes; i++)
writer.Write((byte)0);
}

The result:
Size: 1.00 GB (1,073,741,824 bytes)
Size on disk: 1.00 GB (1,073,741,824 bytes)

Dan Manges
 
Only if you have some sort of hard drive compression enabled.
I just made a 1GB file of all 0's like this:

It's not a sparse file before you mark it as such with
DeviceIoControl(FSCTL_SET_SPARSE).


Mattias
 
Back
Top