By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Size and Size on Disk information.

P: n/a
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

Jun 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Su***********@gmail.com wrote:
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
Jun 26 '06 #2

P: n/a
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.
On Mon, 26 Jun 2006 23:35:07 GMT, Dan Manges <da***********@gmail.com>
wrote:
Su***********@gmail.com wrote:
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

Jun 27 '06 #3

P: n/a
Chris Chilvers wrote:
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
Jun 27 '06 #4

P: n/a
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.