/// <summary> /// Figure out size information from strSize /// </summary> /// <param name="strSize"></param> protected void AnalyzeSize(string strSize) { _sizeUnit = SizeUnits.B; _actualNumber = ulong.MaxValue; ulong multiplier = 1; // terabytes if (strSize[strSize.Length - 2] == 'T' || strSize[strSize.Length - 2] == 't') { _sizeUnit = SizeUnits.TB; multiplier = 1024LU * 1024LU * 1024LU * 1024LU; } // gigabytes else if (strSize[strSize.Length - 2] == 'G' || strSize[strSize.Length - 2] == 'g') { _sizeUnit = SizeUnits.GB; multiplier = 1024LU * 1024LU * 1024LU; } // megabytes else if (strSize[strSize.Length - 2] == 'M' || strSize[strSize.Length - 2] == 'm') { _sizeUnit = SizeUnits.MB; multiplier = 1024LU * 1024LU; } // kilobytes else if (strSize[strSize.Length - 2] == 'K' || strSize[strSize.Length - 2] == 'k') { _sizeUnit = SizeUnits.KB; multiplier = 1024LU; } string strNumber; // if the size is less than 1MB // (ex. "100b") if (multiplier == 1) { strNumber = strSize.Substring(0, strSize.Length - 1); } // (ex. "10TB") else { strNumber = strSize.Substring(0, strSize.Length - 2); } if (float.TryParse(strNumber, out _number)) { _actualNumber = (ulong)(_number * multiplier); _isValid = true; } else { _actualNumber = ulong.MaxValue; _isValid = false; } }
public static double GetConversionFactor(SizeUnits fromUnits, SizeUnits toUnits, bool metric) { double toBytes = 1; double fromBytes = 1; switch (fromUnits) { case SizeUnits.Kilobytes: toBytes = metric ? BYTES_PER_KILOBYTE_METRIC : BYTES_PER_KILOBYTE_BINARY; break; case SizeUnits.Megabytes: toBytes = metric ? BYTES_PER_MEGABYTE_METRIC : BYTES_PER_MEGABYTE_BINARY; break; case SizeUnits.Gigabytes: toBytes = metric ? BYTES_PER_GIGABYTE_METRIC : BYTES_PER_GIGABYTE_BINARY; break; } switch (toUnits) { case SizeUnits.Kilobytes: fromBytes = metric ? 1 / BYTES_PER_KILOBYTE_METRIC : 1 / BYTES_PER_KILOBYTE_BINARY; break; case SizeUnits.Megabytes: fromBytes = metric ? 1 / BYTES_PER_MEGABYTE_METRIC : 1 / BYTES_PER_MEGABYTE_BINARY; break; case SizeUnits.Gigabytes: fromBytes = metric ? 1 / BYTES_PER_GIGABYTE_METRIC : 1 / BYTES_PER_GIGABYTE_BINARY; break; } return(toBytes * fromBytes); }
public static string ToSize(Int64 value, SizeUnits unit) { return(String.Format( "<{0} {1}>", (value / (double)Math.Pow(1024, (Int64)unit)).ToString("0.00"), Enum.GetName(typeof(SizeUnits), unit) )); }
/// <summary> /// Gets the file size display. /// </summary> /// <returns>System.String.</returns> public override long?GetSize(SizeUnits sizeUnits) { // RefreshObject(); if (FileInfo?.Length == null) { return(null); } return(ByteSizeHelper.GetSize(FileInfo.Length, sizeUnits)); }
private static string GetAbbreviation(this SizeUnits sizeUnit) { return(sizeUnit switch { SizeUnits.Byte => "B", SizeUnits.Kilobyte => "KB", SizeUnits.Megabyte => "MB", SizeUnits.Gigabyte => "GB", SizeUnits.Terabyte => "TB", _ => throw new ArgumentException("You managed to pass a wrong enum value!") });
/// <summary> /// Gets the available physical memory. /// </summary> /// <param name="units"></param> /// <returns></returns> public static long GetAvailableMemory(SizeUnits units) { long availableBytes = Convert.ToInt64(MemoryPerformanceCounter.NextValue()); if (units == SizeUnits.Bytes) return availableBytes; else if (units == SizeUnits.Kilobytes) return availableBytes / 1024; else if (units == SizeUnits.Megabytes) return availableBytes / 1048576; else return availableBytes / 1073741824; }
public static string GetUnitSymbol(SizeUnits units) { switch (units) { case SizeUnits.Kilobytes: return("kB"); case SizeUnits.Megabytes: return("MB"); case SizeUnits.Gigabytes: return("GB"); } return("B"); }
public static string ToSize(this Int64 value) { SizeUnits unit = SizeUnits.Bytes; if (value > 1024) { unit = SizeUnits.KB; } else if (value > 1024 * 1024) { unit = SizeUnits.MB; } else { unit = SizeUnits.GB; } return((value / (double)Math.Pow(1024, (Int64)unit)).ToString("0.00 ") + unit.ToString()); }
private Vector2 StandardOffset(Vector2 size, SizeUnits sizeUnits, Vector2 spriteSize, float dpi) { Vector2 offset = new Vector2(); if (sizeUnits == SizeUnits.Pixels) { offset = size; } else if (sizeUnits == SizeUnits.DIPs) { offset = size * dpi / 96.0f; } else if (sizeUnits == SizeUnits.Normalized) { offset = spriteSize * size; } return(offset); }
protected Int64 ConvertSizeToMiB(Int64 size, SizeUnits inputUnit) { switch (inputUnit) { case SizeUnits.B: return((Int64)Math.Ceiling((double)(size / 1024 / 1024))); case SizeUnits.KB: return((Int64)Math.Ceiling((double)(size / 1024))); case SizeUnits.GB: return(size * 1024); case SizeUnits.TB: return(size * 1024 * 1024); } return(size); }
/// <summary> /// Gets the available physical memory. /// </summary> /// <param name="units"></param> /// <returns></returns> public static long GetAvailableMemory(SizeUnits units) { long availableBytes = Convert.ToInt64(MemoryPerformanceCounter.NextValue()); if (units == SizeUnits.Bytes) { return(availableBytes); } else if (units == SizeUnits.Kilobytes) { return(availableBytes / 1024); } else if (units == SizeUnits.Megabytes) { return(availableBytes / 1048576); } else { return(availableBytes / 1073741824); } }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static double GetFileSize(string filePath, SizeUnits units, ref string msg) { FileInfo fileInfo = new FileInfo(filePath); double fileSize = 0; switch (units) { case SizeUnits.bit: break; case SizeUnits.Byte: fileSize = fileInfo.Length; msg = fileSize + "B"; break; case SizeUnits.KByte: fileSize = fileInfo.Length / 1024; msg = fileSize + "KB"; break; case SizeUnits.MByte: fileSize = fileInfo.Length / 1024 / 1024; msg = fileSize + "MB"; break; case SizeUnits.GByte: fileSize = fileInfo.Length / 1024 / 1024 / 1024; msg = fileSize + "GB"; break; case SizeUnits.TByte: fileSize = fileInfo.Length / 1024 / 1024 / 1024 / 1024; msg = fileSize + "TB"; break; default: break; } return(fileSize); }
/// <summary> /// Gets the file size display. /// </summary> /// <returns>System.String.</returns> public static long?GetSize(long bytes, SizeUnits sizeUnits) { if (bytes == 0) { return(0); } const int filter = 1024; if (sizeUnits == SizeUnits.Byte) { return(bytes); } var limit = (int)sizeUnits; var value = bytes; while (limit > 0) { limit--; value /= filter; } return(value); }
public CacheSize(int size, SizeUnits units) { this.Size = size; this.Units = units; }
public static string ConvertToSize(this ulong value, SizeUnits unit) { return((value / (double)Math.Pow(1024, (ulong)unit)).ToString("0.00")); }
public override long?GetSize(SizeUnits sizeUnits) { return(DirectoryInfo.GetFiles("*", SearchOption.AllDirectories).Select(f => new FileObject(f).GetSize(sizeUnits).GetValueOrDefault(0)).Sum()); }
public static string ToSize(long value, SizeUnits unit, bool binary = true) { return((value / (double)Math.Pow((binary ? 1024 : 1000), (Int64)unit)).ToString("0.00") + unit.ToString().Insert(1, (binary ? "i" : ""))); }
public object Convert(object value, Type targetType, object parameter, string language) { SizeUnits units = (SizeUnits)Enum.Parse(typeof(SizeUnits), (string)parameter, true); return($"{((int) value/Math.Pow(1024, (long) units - 1)).ToString("0.00")} {units}"); }
public static double ConvertSize(double value, SizeUnits fromUnits, SizeUnits toUnits, bool metric = false) { return(value * GetConversionFactor(fromUnits, toUnits, metric)); }
public static string ToFileSize(this long size, SizeUnits metric) { var powerToRaiseTo = (int)metric; return(size.ToMetricString(metric, powerToRaiseTo)); }
public static string ToSize(this Int64 value, SizeUnits unit) { return((value / (double)Math.Pow(1024, (Int64)unit)).ToString("0.00")); }
public static double ToSize(this int value, SizeUnits unit) { return(value / (double)Math.Pow(1024, (Int64)unit)); }
public abstract long?GetSize(SizeUnits sizeUnits);
public static decimal ToSizeUnits(this long value, SizeUnits unit) => value / (decimal)Math.Pow(1024, (long)unit);
private Vector2 StandardOffset(Vector2 size, SizeUnits sizeUnits, Vector2 spriteSize, float dpi) { Vector2 offset = new Vector2(); if (sizeUnits == SizeUnits.Pixels) { offset = size; } else if (sizeUnits == SizeUnits.DIPs) { offset = size * dpi / 96.0f; } else if (sizeUnits == SizeUnits.Normalized) { offset = spriteSize * size; } return offset; }
/// <summary> /// Copy constructor /// </summary> public DbSize(DbSize copy) { this.size = copy.Size; this.sizeUnit = copy.SizeUnit; }
/// <summary> /// Converts value to specified size units /// </summary> /// <param name="value">Value to process</param> /// <param name="unit">Size unit</param> /// <returns>String that represents size of the <paramref name="value"/> in specified <paramref name="unit"/>s</returns> public static string ToFileSize(this long value, SizeUnits unit) { return((value / Math.Pow(1024, (long)unit)).ToString("0.00") + unit); }
public DbSize(int size, SizeUnits sizeUnit) { this.size = size; this.sizeUnit = sizeUnit; }
protected Int64 ConvertSizeToMiB(Int64 size, SizeUnits inputUnit) { switch (inputUnit) { case SizeUnits.B: return (Int64) Math.Ceiling((double) (size / 1024 / 1024)); case SizeUnits.KB: return (Int64) Math.Ceiling((double) (size / 1024)); case SizeUnits.GB: return size * 1024; case SizeUnits.TB: return size * 1024 * 1024; } return size; }
public FileSize(int Value, SizeUnits Units) { this.Value = Value; this.Units = Units; }
private static string ToMetricString(this long size, SizeUnits metric, int powerToRaiseTo) { var sizeInProperMetric = size / Math.Pow(1024, powerToRaiseTo); return(sizeInProperMetric.ToString("F0") + metric.GetAbbreviation()); }