ソースコード
public static class FileUtils
{
public static string ToReadableSize( double size, int scale = 0, int standard = 1024 )
{
var unit = new[] { "B", "KB", "MB", "GB" };
if ( scale == unit.Length - 1 || size <= standard )
{
return $"{size.ToString( ".##" )} {unit[ scale ]}";
}
return ToReadableSize( size / standard, scale + 1, standard );
}
}
使い方
var info = new FileInfo( "" );
var size = info.Length;
var readableSize = FileUtils.ToReadableSize( size ) ;
Console.WriteLine( readableSize );
参考サイト様