EnglishEspañolБългарскиРусский


    Partners - Technical discussions - C Sharp get file size with corresponding measure unit
  Posts: 1

Support
Registered:2014-12-14
  2018-08-14 15:08:04 UTC #1

static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };

// Returns file size in corresponding measure unit.

public static string SizeSuffix(Int64 value)

{

if (value < 0)

{

return "-" + SizeSuffix(-value);

}

int i = 0;

decimal dValue = value;

while (Math.Round(dValue / 1024) >= 1)

{

dValue /= 1024; i++;

}

return string.Format("{0:0.##} {1}", dValue, SizeSuffixes[i]);

}