views:

109

answers:

2

I'm writing an app where I want to compare two strings' size. By size, I mean the disk space they would take up if they were directly saved to the disk.

Is it possible to calculate the size of a string in C# without saving it to disk and checking the file information (unless it is more efficient to save it to disk)?

+7  A: 

Yup, it's easy:

Encoding encoding = Encoding.UTF8; // Or whatever
int size = encoding.GetByteCount(text);

Note that this is the number of bytes in the encoded form. If your file system is performing compression etc, it becomes much harder (or impossible) to predict the physical size taken.

You do have to pick an encoding though. There's really no such thing as "directly" saving to disk - you've got to pick a binary representation.

Jon Skeet
In terms of disk space it would probably be in multiples of the cluster size.
Chris Taylor
@Chris: Assuming it's in a file on its own... which may not be the case.
Jon Skeet
@Jon, I think that is my point. Even if the string is appended existing data in the file, if the last byte of the string causes the next cluster to be allocated, that will have a disk size cost of a full cluster.
Chris Taylor
@Chris: Yes, that's true. I suspect it's not what the OP was interested in, but it's worth considering.
Jon Skeet
A: 

When you save data to disk that data is written in clusters, depending on the disk size and the file system the cluster size varies.

For NTFS disk less than 16TB in size the cluster size is 4K, which means that a string of 1 byte will consume a 4K disk space, and a string of 5000 bytes will consume 8K in disk space.

Here is a link with the various MS filesytems and the default cluster sizes http://support.microsoft.com/kb/140365

Chris Taylor