473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Is there any file size format function in c#?

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
Jul 24 '06 #1
6 10171
thi wrote:
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.
Jul 24 '06 #2
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();

Jul 24 '06 #3
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"thi" <th******@hotmail.comwrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
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

Jul 24 '06 #4
thi
Cheer, that work perfectly but is there any pros and cons about this api?

"thi" <th******@hotmail.comwrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
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

Jul 24 '06 #5
Nicholas Paldino [.NET/C# MVP] wrote:
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
Jul 25 '06 #6
thi wrote:
Cheer, that work perfectly but is there any pros and cons about this api?

"thi" <th******@hotmail.comwrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
>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

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.
Jul 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris | last post by:
How do I find out the size of a file. I can get listing of files in a directory but I cannot figure out how to get the file size also. Here is what I have so far. <% Set...
10
by: John Ratliff | last post by:
What is the best way to determine a file's size? Should I use stat? Is this a better way? std::fstream file("myfile", std::ios_base::in | std::ios_base::binary); file.seekg(0,...
7
by: Andrew | last post by:
Hello, Is it possible for a file position indicator of an input stream to be greater than the size of a file? And if so could this cause fgetc to somehow write to the file? For example is it...
7
by: Prince of Code | last post by:
Can anybody help me out by writing a function for the given problem Write a function in PHP that converts given number of bytes into Readabel format like 99.92 KB for 102323 bytes and 3 MB for...
2
by: abcd | last post by:
is there a built-in way of printing the size of a file nicely? So if the file size is 103803 bytes it prints out like: 103.8K or 0.1MB something liek that?
5
by: Jefferis NoSpamme | last post by:
Hi all, I'm trying to limit the file size of an image submission and I keep running into various problems. I've got most of it working, but I'm stumped and I have a basic question as to WHY this...
5
by: ChinChin | last post by:
Hi, I have made a window application that get file information from the server the file size return in bytes. I need help on how to format the file size accordingly so that it will return the file...
9
by: samuraisam | last post by:
Quick file size formatting for all those seekers out there... import math def filesizeformat(bytes, precision=2): """Returns a humanized string for a given amount of bytes""" bytes =...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.