Monday, May 02, 2011

My cool found API call of the X: StrFormatByteSize (convert file size into human readable text)

VB Helper - Make extension methods to convert file sizes into strings giving sizes in KB, MB, etc. in Visual Basic .NET

"Title: Make extension methods to convert file sizes into strings giving sizes in KB, MB, etc. in Visual Basic .NET

Description: This example shows how to make extension methods to convert file sizes into strings giving sizes in KB, MB, etc. in Visual Basic .NET.

...

This example defines two extension methods for converting a file size in bytes into a value ending with an appropriate size such as KB, MB, or GB.

The first method uses the StrFormatByteSize API function.

..."

Funny the stuff you learn. I've got a code snip that I use everywhere for converting file sizes into a short human readable string. Any until today never knew there was a Windows API call, that's been available since Win2000, that does the same thing, StrFormatByteSize(A, W, EX, 64)

"Converts a numeric value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes, depending on the size. ...

The first parameter of this function has a different type for the ANSI and Unicode versions. If your numeric value is a DWORD, you can use StrFormatByteSize with text macros for both cases. The compiler will cast the numerical value to a LONGLONG for the Unicode case. If your numerical value is a LONGLONG, you should use StrFormatByteSizeW explicitly

532 -> 532 bytes 
1340 -> 1.3KB 
23506 -> 23.5KB 
2400016 -> 2.4MB
2400000000 -> 2.4GB

..."

LTL (Love To Learn  :) 

No comments: