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 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.
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 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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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 =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |