Mega Code Archive

 
Categories / C# / Data Types
 

Convert Size to String

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace NetworkAssetManager.WMI {     class WMIUtil     {         public static string ConvertSizetoString(UInt64 value, bool rounded, Units unit)         {             const double KB = 1024.0;             const double MB = 1048576.0;             const double GB = 1000000000.0;             const double TB = 1000000000000.0;             double dValue = 0;             string retConv = string.Empty;             string format = string.Empty;             if (rounded == true)             {                 format = "{0:0}";             }             else             {                 format = "{0:0.##}";             }             if (value >= KB && value <= MB)             {                 dValue = (value / KB);                 retConv = String.Format(format, dValue) + " KBs";             }             else if (value >= MB && value <= GB)             {                 dValue = (value / MB);                 retConv = String.Format(format, dValue) + " MBs";             }             else if (value >= GB && value <= TB)             {                 dValue = (value / GB);                 retConv = String.Format(format, dValue) + " GBs";             }             else if (value >= TB)             {                 dValue = (value / TB);                 retConv = String.Format(format, dValue) + " KBs";             }             else             {                 retConv = value.ToString() + " bytes";             }             return retConv;         }     } }