/// <summary> /// Formats <paramref name="bytes"/> into a textual representation using the specified <paramref name="format"/>. /// </summary> /// <param name="bytes">The bytes to format.</param> /// <param name="format">The format - eg. <see cref="FileSizeFormat.Kibi"/> or <see cref="FileSizeFormat.Kilo"/>.</param> /// <param name="culture">The culture to be used when formatting the file size.</param> /// <returns>A string representing the formatted file size.</returns> public static string FormatFileSize(long bytes, FileSizeFormat format, CultureInfo culture) { return(format switch { FileSizeFormat.Kibi => FormatFileSizeInternal(bytes, 1024, _fileSizesKibi, culture), FileSizeFormat.Kilo => FormatFileSizeInternal(bytes, 1000, _fileSizesKilo, culture), _ => FormatFileSizeInternal(bytes, 1024, _fileSizesKilo, culture) });
public static string FileSize(this ulong value, FileSizeFormat format, int round = 1) { if (format == FileSizeFormat.Bytes) { return(value.ToString()); } var Labels = new string[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" }; if (format == FileSizeFormat.BinaryUsingSI || format == FileSizeFormat.DecimalUsingSI) { Labels = new string[] { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; } if (value == 0) { return("0 B"); } var f = format == FileSizeFormat.BinaryUsingSI || format == FileSizeFormat.IECBinary ? (ulong)1024 : 1000; var m = (int)System.Math.Log(value, f); var a = (decimal)value / (1L << (m * 10)); if (System.Math.Round(a, round) >= 1000) { m += 1; a /= f; } var result = string.Format("{0:n" + round + "}", a); var j = result.Length; for (var i = result.Length - 1; i >= 0; i--) { if (result[i] == '.') { j--; break; } if (result[i] == '0') { j--; } else { break; } } return($"{result.Substring(0, j)} {Labels[m]}");; }
/// <summary> /// /// </summary> /// <param name="Bytes"></param> /// <param name="FileSizeFormat"></param> /// <param name="RoundTo"></param> /// <returns></returns> public static string ToFileSize(this long Bytes, FileSizeFormat FileSizeFormat = FileSizeFormat.BinaryUsingSI, int RoundTo = 2) { if (FileSizeFormat == FileSizeFormat.Bytes) { return(Bytes.ToString()); } var Format = FileSizeFormat == FileSizeFormat.BinaryUsingSI || FileSizeFormat == FileSizeFormat.IECBinary ? 1024L : 1000L; var Labels = new string[, ] { { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" }, { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }, }; var f = FileSizeFormat == FileSizeFormat.BinaryUsingSI || FileSizeFormat == FileSizeFormat.DecimalUsingSI ? 1 : 0; var Result = -1d; for (var i = 8; i >= 0; i--) { if (i == 0) { Result = Bytes; } else if (i == 1 && Bytes >= Format) { Result = Bytes / Format; } else { var k = Math.Pow(Format, i); Result = Bytes >= k ? Bytes / k : Result; } if (Result >= 0) { return("{0} {1}".F(Result.Round(RoundTo), Labels[f, i])); } } return(string.Empty); }
/// <summary> /// /// </summary> /// <param name="value"></param> /// <param name="format"></param> /// <param name="round"></param> /// <returns></returns> public static string ToFileSize(this UInt64 value, FileSizeFormat format, int round = 1) { if (format == FileSizeFormat.Bytes) { return(value.ToString()); } var Labels = new string[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" }; if (format == FileSizeFormat.BinaryUsingSI || format == FileSizeFormat.DecimalUsingSI) { Labels = new string[] { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; } if (value == 0) { return("0 B"); } var f = format == FileSizeFormat.BinaryUsingSI || format == FileSizeFormat.IECBinary ? (ulong)1024 : 1000; var m = (int)Math.Log(value, f); var a = (decimal)value / (1L << (m * 10)); if (Math.Round(a, round) >= 1000) { m += 1; a /= f; } return(string.Format("{0:n" + round + "} {1}", a, Labels[m])); }
public FolderListViewItem(Pidl absolutePidl, Pidl myDocumentsReferencePidl, FileSizeFormat fileSizeFormat) { _pidl = absolutePidl; _fullPath = absolutePidl.Path; // request shell item info now - it's faster than binding to ShellItem directly, and we need it for sorting purposes anyway var uFlags = SHGFI.SHGFI_PIDL | SHGFI.SHGFI_TYPENAME | SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_SYSICONINDEX; // bug #9975: asking for SHGFI_ATTRIBUTES can be a very slow operation, so we don't want to ask for them unless we really need them. // if we're dealing with a regular file or directory, then getting the attributes gives us no additional information, so we can avoid it var isFile = !string.IsNullOrEmpty(_fullPath) && File.Exists(_fullPath); var isDirectory = !string.IsNullOrEmpty(_fullPath) && Directory.Exists(_fullPath); if (!isFile && !isDirectory) { uFlags = uFlags | SHGFI.SHGFI_ATTRIBUTES; } var shInfo = new SHFILEINFO(); Shell32.SHGetFileInfo((IntPtr) _pidl, 0, out shInfo, (uint)Marshal.SizeOf(shInfo), uFlags); // check if item is purely virtual or is a physical file system object var isPureVirtual = !isFile && !isDirectory && ((SFGAO)shInfo.dwAttributes & SFGAO.SFGAO_FILESYSTEM) == 0; // check if item is actually a folder or potentially has subfolders; exclude real files that provide subfolders via shell namespace extensions (e.g. ZIP files in WinXP) var isFolder = isDirectory || (((SFGAO) shInfo.dwAttributes & (SFGAO.SFGAO_FOLDER | SFGAO.SFGAO_HASSUBFOLDER)) != 0 && !isFile); byte itemClass; if (_pidl == myDocumentsReferencePidl) itemClass = 0; else if (isPureVirtual && isFolder) itemClass = 64; else if (isFolder) itemClass = (byte) (128 + GetRootVolumeIndex(_fullPath)); else itemClass = byte.MaxValue; this._iconIndex = shInfo.iIcon; this._lastModifiedValid = false; this.DisplayName = shInfo.szDisplayName; this.TypeName = shInfo.szTypeName; this.ItemClass = itemClass; this.LastModified = DateTime.MinValue; this.IsFolder = isFolder; this.FileSize = -1; if (File.Exists(_fullPath)) { FileInfo fileInfo = new FileInfo(this._fullPath); this._lastModifiedValid = true; this.LastModified = fileInfo.LastWriteTime; this.FileSize = fileInfo.Length; } else if (Directory.Exists(_fullPath)) { this._lastModifiedValid = true; this.LastModified = Directory.GetLastWriteTime(_fullPath); } this.Text = this.DisplayName; this.ImageIndex = this._iconIndex; this.SubItems.Add(CreateSubItem(this, FormatFileSize(this.FileSize, fileSizeFormat))); this.SubItems.Add(CreateSubItem(this, this.TypeName)); this.SubItems.Add(CreateSubItem(this, FormatLastModified(this.LastModified, this._lastModifiedValid))); }
private void ResetFileSizeFormat() { this.FileSizeFormat = FileSizeFormat.BinaryOctets; }
public static string FileSize(this long value, FileSizeFormat format, int round = 1) => value.Coerce(long.MaxValue).UInt64().FileSize(format, round);
/// <summary> /// Formats <paramref name="bytes"/> into a textual representation using the specified <paramref name="format"/>. /// </summary> /// <param name="bytes">The bytes to format.</param> /// <param name="format">The format - eg. <see cref="FileSizeFormat.Kibi"/> or <see cref="FileSizeFormat.Kilo"/>.</param> /// <returns>A string representing the formatted file size.</returns> public static string FormatFileSize(long bytes, FileSizeFormat format) { return(FormatFileSize(bytes, format, CultureInfo.CurrentCulture)); }
public Thumbnail(string path, DateTime date, long size) { Path = path; Date = date; Size = FileSizeFormat.FormatBytes(size, 2); }
public FolderListViewItem(Pidl absolutePidl, Pidl myDocumentsReferencePidl, FileSizeFormat fileSizeFormat) { _pidl = absolutePidl; _fullPath = absolutePidl.Path; // request shell item info now - it's faster than binding to ShellItem directly, and we need it for sorting purposes anyway var uFlags = SHGFI.SHGFI_PIDL | SHGFI.SHGFI_TYPENAME | SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_SYSICONINDEX; // bug #9975: asking for SHGFI_ATTRIBUTES can be a very slow operation, so we don't want to ask for them unless we really need them. // if we're dealing with a regular file or directory, then getting the attributes gives us no additional information, so we can avoid it var isFile = !string.IsNullOrEmpty(_fullPath) && File.Exists(_fullPath); var isDirectory = !string.IsNullOrEmpty(_fullPath) && Directory.Exists(_fullPath); if (!isFile && !isDirectory) { uFlags = uFlags | SHGFI.SHGFI_ATTRIBUTES; } var shInfo = new SHFILEINFO(); Shell32.SHGetFileInfo((IntPtr)_pidl, 0, out shInfo, (uint)Marshal.SizeOf(shInfo), uFlags); // check if item is purely virtual or is a physical file system object var isPureVirtual = !isFile && !isDirectory && ((SFGAO)shInfo.dwAttributes & SFGAO.SFGAO_FILESYSTEM) == 0; // check if item is actually a folder or potentially has subfolders; exclude real files that provide subfolders via shell namespace extensions (e.g. ZIP files in WinXP) var isFolder = isDirectory || (((SFGAO)shInfo.dwAttributes & (SFGAO.SFGAO_FOLDER | SFGAO.SFGAO_HASSUBFOLDER)) != 0 && !isFile); byte itemClass; if (_pidl == myDocumentsReferencePidl) { itemClass = 0; } else if (isPureVirtual && isFolder) { itemClass = 64; } else if (isFolder) { itemClass = (byte)(128 + GetRootVolumeIndex(_fullPath)); } else { itemClass = byte.MaxValue; } this._iconIndex = shInfo.iIcon; this._lastModifiedValid = false; this.DisplayName = shInfo.szDisplayName; this.TypeName = shInfo.szTypeName; this.ItemClass = itemClass; this.LastModified = DateTime.MinValue; this.IsFolder = isFolder; this.FileSize = -1; if (File.Exists(_fullPath)) { FileInfo fileInfo = new FileInfo(this._fullPath); this._lastModifiedValid = true; this.LastModified = fileInfo.LastWriteTime; this.FileSize = fileInfo.Length; } else if (Directory.Exists(_fullPath)) { this._lastModifiedValid = true; this.LastModified = Directory.GetLastWriteTime(_fullPath); } this.Text = this.DisplayName; this.ImageIndex = this._iconIndex; this.SubItems.Add(CreateSubItem(this, FormatFileSize(this.FileSize, fileSizeFormat))); this.SubItems.Add(CreateSubItem(this, this.TypeName)); this.SubItems.Add(CreateSubItem(this, FormatLastModified(this.LastModified, this._lastModifiedValid))); }
public static string FormatFileSize(long SizeBytes, FileSizeFormat Format) { string Suffix = ""; string SuffixSingular = ""; long SizeToUse = SizeBytes; switch (Format) { case FileSizeFormat.SizeBytes: Suffix = "B"; SuffixSingular = "Bytes"; break; case FileSizeFormat.SizeBits: Suffix = "b"; SuffixSingular = "bits"; SizeToUse = SizeToUse * 8; break; case FileSizeFormat.SpeedBytes: Suffix = "B/s"; SuffixSingular = "Bytes/s"; break; case FileSizeFormat.SpeedBits: Suffix = "b/s"; SuffixSingular = "bits/s"; SizeToUse = SizeToUse * 8; break; default: break; } if (SizeToUse >= 1125899906842624) { Decimal size = Decimal.Divide(SizeToUse, 1125899906842624); return String.Format("{0:##.##} P{1}", size, Suffix); } if (SizeToUse >= 1099511627776) { Decimal size = Decimal.Divide(SizeToUse, 1099511627776); return String.Format("{0:##.##} T{1}", size, Suffix); } if (SizeToUse >= 1073741824) { Decimal size = Decimal.Divide(SizeToUse, 1073741824); return String.Format("{0:##.##} G{1}", size, Suffix); } else if (SizeToUse >= 1048576) { Decimal size = Decimal.Divide(SizeToUse, 1048576); return String.Format("{0:##.##} M{1}", size, Suffix); } else if (SizeToUse >= 1024) { Decimal size = Decimal.Divide(SizeToUse, 1024); return String.Format("{0:##.##} K{1}", size, Suffix); } else if (SizeToUse > 0 & SizeToUse < 1024) { Decimal size = SizeToUse; return String.Format("{0:##.##} {1}", size, SuffixSingular); } else { return String.Format("0 {0}", SuffixSingular); } }
public static string FormatFileSize(long SizeBytes, FileSizeFormat Format) { string Suffix = ""; string SuffixSingular = ""; long SizeToUse = SizeBytes; switch (Format) { case FileSizeFormat.SizeBytes: Suffix = "B"; SuffixSingular = "Bytes"; break; case FileSizeFormat.SizeBits: Suffix = "b"; SuffixSingular = "bits"; SizeToUse = SizeToUse * 8; break; case FileSizeFormat.SpeedBytes: Suffix = "B/s"; SuffixSingular = "Bytes/s"; break; case FileSizeFormat.SpeedBits: Suffix = "b/s"; SuffixSingular = "bits/s"; SizeToUse = SizeToUse * 8; break; default: break; } if (SizeToUse >= 1125899906842624) { Decimal size = Decimal.Divide(SizeToUse, 1125899906842624); return(String.Format("{0:##.##} P{1}", size, Suffix)); } if (SizeToUse >= 1099511627776) { Decimal size = Decimal.Divide(SizeToUse, 1099511627776); return(String.Format("{0:##.##} T{1}", size, Suffix)); } if (SizeToUse >= 1073741824) { Decimal size = Decimal.Divide(SizeToUse, 1073741824); return(String.Format("{0:##.##} G{1}", size, Suffix)); } else if (SizeToUse >= 1048576) { Decimal size = Decimal.Divide(SizeToUse, 1048576); return(String.Format("{0:##.##} M{1}", size, Suffix)); } else if (SizeToUse >= 1024) { Decimal size = Decimal.Divide(SizeToUse, 1024); return(String.Format("{0:##.##} K{1}", size, Suffix)); } else if (SizeToUse > 0 & SizeToUse < 1024) { Decimal size = SizeToUse; return(String.Format("{0:##.##} {1}", size, SuffixSingular)); } else { return(String.Format("0 {0}", SuffixSingular)); } }
/// <summary> /// /// </summary> /// <param name="Bytes"></param> /// <param name="FileSizeFormat"></param> /// <param name="RoundTo"></param> /// <returns></returns> public static string ToFileSize(this int Bytes, FileSizeFormat FileSizeFormat = FileSizeFormat.BinaryUsingSI, int RoundTo = 2) { return(Bytes.ToInt64().ToFileSize(FileSizeFormat, RoundTo)); }
public static string FormatDirectoryBytesSize( this DirectoryInfo info, FileSizeFormat format);
private static string FormatFileSize(long fileSize, FileSizeFormat fileSizeFormat) { if (fileSize < 0) // file doesn't exist! return string.Empty; switch (fileSizeFormat) { case FileSizeFormat.MetricOctets: if (fileSize < 900) // less than 900 bytes return string.Format(SR.FormatFileSizeBytes, fileSize); if (fileSize < 900000) // between 900 bytes and 900 KB return string.Format(SR.FormatFileSizeKB, fileSize/1000.0); else if (fileSize < 900000000) // between 900 KB and 900 MB return string.Format(SR.FormatFileSizeMB, fileSize/1000000.0); else if (fileSize < 900000000000) // between 900 MB and 900 GB return string.Format(SR.FormatFileSizeGB, fileSize/1000000000.0); else // greater than 900 GB... return string.Format(SR.FormatFileSizeTB, fileSize/1000000000000.0); case FileSizeFormat.LegacyOctets: if (fileSize < 896) // less than 896 bytes return string.Format(SR.FormatFileSizeBytes, fileSize); if (fileSize < 917504) // between 896 bytes and 896 KiB return string.Format(SR.FormatFileSizeKB, fileSize/1024.0); else if (fileSize < 939524096) // between 896 KiB and 896 MiB return string.Format(SR.FormatFileSizeMB, fileSize/1048576.0); else if (fileSize < 841813590016) // between 896 MiB and 896 GiB return string.Format(SR.FormatFileSizeGB, fileSize/1073741824.0); else // greater than 896 GiB... return string.Format(SR.FormatFileSizeTB, fileSize/1099511627776.0); case FileSizeFormat.BinaryOctets: default: if (fileSize < 896) // less than 896 bytes return string.Format(SR.FormatFileSizeBytes, fileSize); if (fileSize < 917504) // between 896 bytes and 896 KiB return string.Format(SR.FormatFileSizeKiB, fileSize/1024.0); else if (fileSize < 939524096) // between 896 KiB and 896 MiB return string.Format(SR.FormatFileSizeMiB, fileSize/1048576.0); else if (fileSize < 841813590016) // between 896 MiB and 896 GiB return string.Format(SR.FormatFileSizeGiB, fileSize/1073741824.0); else // greater than 896 GiB... return string.Format(SR.FormatFileSizeTiB, fileSize/1099511627776.0); } }
private static string FormatFileSize(long fileSize, FileSizeFormat fileSizeFormat) { if (fileSize < 0) // file doesn't exist! { return(string.Empty); } switch (fileSizeFormat) { case FileSizeFormat.MetricOctets: if (fileSize < 900) // less than 900 bytes { return(string.Format(SR.FormatFileSizeBytes, fileSize)); } if (fileSize < 900000) // between 900 bytes and 900 KB { return(string.Format(SR.FormatFileSizeKB, fileSize / 1000.0)); } else if (fileSize < 900000000) // between 900 KB and 900 MB { return(string.Format(SR.FormatFileSizeMB, fileSize / 1000000.0)); } else if (fileSize < 900000000000) // between 900 MB and 900 GB { return(string.Format(SR.FormatFileSizeGB, fileSize / 1000000000.0)); } else // greater than 900 GB... { return(string.Format(SR.FormatFileSizeTB, fileSize / 1000000000000.0)); } case FileSizeFormat.LegacyOctets: if (fileSize < 896) // less than 896 bytes { return(string.Format(SR.FormatFileSizeBytes, fileSize)); } if (fileSize < 917504) // between 896 bytes and 896 KiB { return(string.Format(SR.FormatFileSizeKB, fileSize / 1024.0)); } else if (fileSize < 939524096) // between 896 KiB and 896 MiB { return(string.Format(SR.FormatFileSizeMB, fileSize / 1048576.0)); } else if (fileSize < 841813590016) // between 896 MiB and 896 GiB { return(string.Format(SR.FormatFileSizeGB, fileSize / 1073741824.0)); } else // greater than 896 GiB... { return(string.Format(SR.FormatFileSizeTB, fileSize / 1099511627776.0)); } case FileSizeFormat.BinaryOctets: default: if (fileSize < 896) // less than 896 bytes { return(string.Format(SR.FormatFileSizeBytes, fileSize)); } if (fileSize < 917504) // between 896 bytes and 896 KiB { return(string.Format(SR.FormatFileSizeKiB, fileSize / 1024.0)); } else if (fileSize < 939524096) // between 896 KiB and 896 MiB { return(string.Format(SR.FormatFileSizeMiB, fileSize / 1048576.0)); } else if (fileSize < 841813590016) // between 896 MiB and 896 GiB { return(string.Format(SR.FormatFileSizeGiB, fileSize / 1073741824.0)); } else // greater than 896 GiB... { return(string.Format(SR.FormatFileSizeTiB, fileSize / 1099511627776.0)); } } }
public static string FormatAsFileSize( this long fileSize, FileSizeFormat format);