Q:
Bagaimana mem-format Filesize agar lebih 'Human Readable'?
A:
Caranya tentu menghitung dan membagi. :)
Nah ada class yang bisa bantu-bantu.
Cara pakaipun simple:
Console.WriteLine(String.Format(New FileSizeFormatProvider(), "File size: {0:fs}", fInfo.Length))
Code di atas memerlukan code di bawah.
Class FileSizeFormatProvider
// C#
public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter)) return this;
return null;
}
private const string fileSizeFormat = "fs";
private const Decimal OneKiloByte = 1024M;
private const Decimal OneMegaByte = OneKiloByte * 1024M;
private const Decimal OneGigaByte = OneMegaByte * 1024M;
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (format == null || !format.StartsWith(fileSizeFormat))
{
return defaultFormat(format, arg, formatProvider);
}
if (arg is string)
{
return defaultFormat(format, arg, formatProvider);
}
Decimal size;
try
{
size = Convert.ToDecimal(arg);
}
catch (InvalidCastException)
{
return defaultFormat(format, arg, formatProvider);
}
string suffix;
if (size > OneGigaByte)
{
size /= OneGigaByte;
suffix = "GB";
}
else if (size > OneMegaByte)
{
size /= OneMegaByte;
suffix = "MB";
}
else if (size > OneKiloByte)
{
size /= OneKiloByte;
suffix = "kB";
}
else
{
suffix = " B";
}
string precision = format.Substring(2);
if (String.IsNullOrEmpty(precision)) precision = "2";
return String.Format("{0:N" + precision + "}{1}", size, suffix);
}
private static string defaultFormat(string format, object arg, IFormatProvider formatProvider)
{
IFormattable formattableArg = arg as IFormattable;
if (formattableArg != null) return formattableArg.ToString(format, formatProvider);
return arg.ToString();
}
}
code:
' VB.Net
Public Class FileSizeFormatProvider
Implements IFormatProvider, ICustomFormatter
Private Const fileSizeFormat As String = "fs"
Private Shared ReadOnly OneKiloByte As Decimal = 1024
Private Shared ReadOnly OneMegaByte As Decimal = 1048576
Private Shared ReadOnly OneGigaByte As Decimal = 1073741824
Private Shared Function defaultFormat(ByVal format As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String
Dim formattableArg As IFormattable = TryCast(arg, IFormattable)
If (Not formattableArg Is Nothing) Then : Return formattableArg.ToString(format, formatProvider) : End If
Return arg.ToString
End Function
Public Function Format(ByVal formatString As String, ByVal arg As Object, ByVal formatProvider As System.IFormatProvider) As String Implements ICustomFormatter.Format
Dim size As Decimal
Dim suffix As String
If Not ((Not formatString Is Nothing) AndAlso formatString.StartsWith("fs")) Then
Return FileSizeFormatProvider.defaultFormat(formatString, arg, formatProvider)
End If
If TypeOf arg Is String Then
Return FileSizeFormatProvider.defaultFormat(formatString, arg, formatProvider)
End If
Try
size = Convert.ToDecimal(arg)
Catch exception1 As InvalidCastException
Return FileSizeFormatProvider.defaultFormat(formatString, arg, formatProvider)
End Try
If (size > 1073741824) Then
size = (size / 1073741824)
suffix = "GB"
ElseIf (size > 1048576) Then
size = (size / 1048576)
suffix = "MB"
ElseIf (size > 1024) Then
size = (size / 1024)
suffix = "kB"
Else
suffix = " B"
End If
Dim precision As String = formatString.Substring(2)
If String.IsNullOrEmpty(precision) Then : precision = "2" : End If
Return String.Format(("{0:N" & precision & "}{1}"), size, suffix)
End Function
Public Function GetFormat(ByVal formatType As System.Type) As Object Implements System.IFormatProvider.GetFormat
If (formatType Is GetType(ICustomFormatter)) Then
Return Me
End If
Return Nothing
End Function
End Class