.Net Easy File-Size Format Using API

here is a really nice way to skip all those custom-made functions that divides again and again the file size from bytes to get the human-readable file sizes:

C# example

[DllImport("Shlwapi.dll", CharSet = CharSet.Auto)]
public static extern long StrFormatByteSize( long fileSize, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer, int bufferSize );

public static string getPrettyFileSize (long lSize){
	StringBuilder sb = new StringBuilder( 12 );
	StrFormatByteSize( lSize, sb, sb.Capacity );
	return sb.ToString();
}

calling it will result with something like this:

Leave a Reply