Mega Code Archive

 
Categories / C# / File Stream
 

Convert to easy-to-read byte value

using System; using System.Collections.Generic; using System.Text; public class Utils {     public Utils() { }     public static string ConvertBytes(Int64 byteCount)     {         string size = "0 Bytes";         if (byteCount >= 1073741824)             size = String.Format("{0:##.##}", byteCount / 1073741824) + " GB";         else if (byteCount >= 1048576)             size = String.Format("{0:##.##}", byteCount / 1048576) + " MB";         else if (byteCount >= 1024)             size = String.Format("{0:##.##}", byteCount / 1024) + " KB";         else if (byteCount > 0 && byteCount < 1024)             size = byteCount.ToString() + " Bytes";         return size;     }     public static Int64 ConvertToBytes(int amount, string unit)     {         switch (unit.ToLower(System.Globalization.CultureInfo.CurrentCulture))         {             case "gb":                 return ((Int64)amount) * 1073741824;             case "mb":                 return ((Int64)amount) * 1048576;             case "kb":                 return ((Int64)amount) * 1024;             default:                 return 0;         }     }     public static long ConvertBytes(Int64 valueCount, string unit)     {         switch (unit.ToLower(System.Globalization.CultureInfo.CurrentCulture))         {             case "gb":                 return valueCount / 1073741824;             case "mb":                 return valueCount / 1048576;             case "kb":                 return valueCount / 1024;             default:                 return 0;         }     } }