Advertisement
uwekeim

FileSizeExtensions.cs

Oct 27th, 2020 (edited)
2,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. namespace Extensions
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.     using System.Threading;
  6.  
  7.     public static class FileSizeExtensions
  8.     {
  9.         public static string FormatFileSize(
  10.             this long fileSize,
  11.             Func<double, RoundType, int, object> round = null)
  12.         {
  13.             return FormatFileSize((ulong) fileSize, round);
  14.         }
  15.  
  16.         public enum RoundType
  17.         {
  18.             B,
  19.             KB,
  20.             MB,
  21.             GB,
  22.             TB
  23.         }
  24.  
  25.         private static CultureInfo culture => Thread.CurrentThread.CurrentCulture;
  26.  
  27.         public static string FormatFileSize(
  28.             this ulong fileSize,
  29.             Func<double, RoundType, int, object> round = null)
  30.         {
  31.             const long fileSize1KB = 1024;
  32.             const long fileSize100KB = 102400;
  33.             const long fileSize1MB = 1048576;
  34.             const long fileSize1GB = 1073741824;
  35.             const long fileSize1TB = 1099511627776;
  36.  
  37.             if (fileSize < fileSize1KB)
  38.             {
  39.                 return string.Format(
  40.                     culture,
  41.                     @"{0} {1}",
  42.                     checkround(round, 0, RoundType.B, fileSize),
  43.                     "Bytes");
  44.             }
  45.             else if (fileSize < fileSize100KB)
  46.             {
  47.                 return string.Format(
  48.                     culture,
  49.                     @"{0} {1}",
  50.                     checkround(round, 0, RoundType.KB, fileSize / (double) fileSize1KB),
  51.                     "KB");
  52.             }
  53.             else if (fileSize < fileSize1MB)
  54.             {
  55.                 return string.Format(
  56.                     culture,
  57.                     @"{0} {1}",
  58.                     checkround(round, 0, RoundType.KB, (double) fileSize / fileSize1KB),
  59.                     "KB");
  60.             }
  61.             else if (fileSize < fileSize1GB)
  62.             {
  63.                 return string.Format(
  64.                     culture,
  65.                     @"{0} {1}",
  66.                     checkround(round, 1, RoundType.MB, fileSize / (double) fileSize1MB),
  67.                     "MB");
  68.             }
  69.             else if (fileSize < fileSize1TB)
  70.             {
  71.                 return string.Format(
  72.                     culture,
  73.                     @"{0} {1}",
  74.                     checkround(round, 1, RoundType.GB, fileSize / (double) fileSize1GB),
  75.                     "GB");
  76.             }
  77.             else
  78.             {
  79.                 return string.Format(
  80.                     culture,
  81.                     @"{0} {1}",
  82.                     checkround(round, 2, RoundType.TB, fileSize / (double) fileSize1TB),
  83.                     "TB");
  84.             }
  85.         }
  86.  
  87.         private static object checkround(
  88.             Func<double, RoundType, int, object> round,
  89.             int decimals,
  90.             RoundType roundType,
  91.             double size)
  92.         {
  93.             return round == null
  94.                 ? (decimals > 0 ? Math.Round(size, decimals) : (ulong) size)
  95.                 : round(size, roundType, decimals);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement