Is there any file size format function in c#?

  • Thread starter Thread starter thi
  • Start date Start date
T

thi

Hi,

Just wondering whether there is any filesize format function in c#? The
reason i ask because i want to display the file size just like windows e.g.
100 KB, 1MB, 1GB etc...

I am kind of hoping there is a method already built in c# as currently i
have to write a custom one and it looks like this:

private string formatsizekb(double dsize)
{
const int iKB = 1024;
const long lMB = 1048576;
...

if (dsize < iKB)
return string.format("{0} bytes", dsize);

if (dsize >= iKB && dsize < lMB)
return string.format("{0} KB", dsize/1024);
...

}

This works fine but just thought if there is any better way of doing this.

Thanks in advance
 
thi said:
Hi,

Just wondering whether there is any filesize format function in c#? The
reason i ask because i want to display the file size just like windows e.g.
100 KB, 1MB, 1GB etc...

I am kind of hoping there is a method already built in c# as currently i
have to write a custom one and it looks like this:

private string formatsizekb(double dsize)
{
const int iKB = 1024;
const long lMB = 1048576;
...

if (dsize < iKB)
return string.format("{0} bytes", dsize);

if (dsize >= iKB && dsize < lMB)
return string.format("{0} KB", dsize/1024);
...

}

This works fine but just thought if there is any better way of doing this.

Thanks in advance
Nope, thou must roll thy own :)

I had to do the same thing for processing filesizes retrieved via FileInfo.
 
No, but it would be a good way to demonstrate extension methods in C#
3.0.

public static class Extensions
{
const int iKB = 1024;
const long lMB = 1048576;

public static int ToSizeFormat(this double d)
{
if (dsize < iKB)
return string.format("{0} bytes", dsize);

if (dsize >= iKB && dsize < lMB)
return string.format("{0} KB", dsize/1024);
}
}

string formatted = theDouble.ToSizeFormat();
 
With all due respect to the answers provided by John B and Nick, they
are wrong. You need to call the StrFormatByteSize (StrFormatByteSizeA,
StrFormatByteSizeW or StrFormatByteSize64) function exported from
shlwapi.dll. It will format the string the way the OS does.

Hope this helps.
 
Nicholas said:
With all due respect to the answers provided by John B and Nick, they
are wrong. You need to call the StrFormatByteSize (StrFormatByteSizeA,
StrFormatByteSizeW or StrFormatByteSize64) function exported from
shlwapi.dll. It will format the string the way the OS does.

Hope this helps.

Doh! learn something new every day :)

Thanks Nic

JB
 
thi said:
Cheer, that work perfectly but is there any pros and cons about this api?

sure it will work on windows only and your assembly needs full thrust to
be able to call native methods.

one should always try to avoid native methods, especially if the
functionality needed is merely trivial and can be coded by yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top