![]() |
VOOZH | about |
dotnet add package ByteSize --version 2.1.2
NuGet\Install-Package ByteSize -Version 2.1.2
<PackageReference Include="ByteSize" Version="2.1.2" />
<PackageVersion Include="ByteSize" Version="2.1.2" />Directory.Packages.props
<PackageReference Include="ByteSize" />Project file
paket add ByteSize --version 2.1.2
#r "nuget: ByteSize, 2.1.2"
#:package ByteSize@2.1.2
#addin nuget:?package=ByteSize&version=2.1.2Install as a Cake Addin
#tool nuget:?package=ByteSize&version=2.1.2Install as a Cake Tool
ByteSize is a utility class that makes byte size representation in code easier
by removing ambiguity of the value being represented.
ByteSize is to bytes what System.TimeSpan is to time.
dotnet builddotnet testBy default ByteSize now assumes 1 KB == 1000 B and 1 KiB == 1024 B to
adhere to the IEC and NIST standards (https://en.wikipedia.org/wiki/Binary_prefix).
In version 1 ByteSize assumed 1 KB == 1024 B, that means if you're upgrading
from v1, you'll see differences in values.
When you upgrade an existing application to v2 your existing code will be using
the decimal representation of bytes (i.e. 1 KB == 1000 B). If the difference
in calculation is not material to your application, you don't need to change anything.
However, if you want to use 1 KiB == 1024 B, then you'll need to change all
ByteSize calls to the respective method. For example, calls to
ByteSize.FromKiloByte need to be changed to ByteSize.FromKibiByte.
Lastly, ByteSize no longer supports the ratio of 1 KB == 1024 B. Note this
is kilobytes to bytes. The only ratio of 1 == 1024 is kibibytes
to bytes.
LargestWholeNumberSymbol and LargestWholeNumberValue to LargestWholeNumberDecimalSymbol and LargestWholeNumberDecimalValue respectively.netstandard1.0 and net45.ByteSize adheres to the IEC standard, see this Wikipedia article.
That means ByteSize assumes:
1 kilobyte = 1000 bytes with 2 letter abbrevations b, B,KB, MB, GB, TB, PB.1 kibibyte = 1024 bytes with 3 letter abbrevations b, B,KiB, MiB, GiB, TiB, PiB.ByteSize manages conversion of the values internally and provides methods to create and retrieve the values as needed. See the examples below.
Without ByteSize:
double maxFileSizeMBs = 1.5;
// I need it in KBs and KiBs!
var kilobytes = maxFileSizeMBs * 1000; // 1500
var kibibytes = maxFileSizeMBs * 1024; // 1536
With ByteSize:
var maxFileSize = ByteSize.FromMegaBytes(1.5);
// I have it in KBs and KiBs!!
maxFileSize.KiloBytes; // 1500
maxFileSize.KibiBytes; // 1464.84376
ByteSize behaves like any other struct backed by a numerical value allowing arithmetic operations between two objects.
// Add
var monthlyUsage = ByteSize.FromGigaBytes(10);
var currentUsage = ByteSize.FromMegaBytes(512);
ByteSize total = monthlyUsage + currentUsage;
total.Add(ByteSize.FromKiloBytes(10));
total.AddGigaBytes(10);
// Subtract
var delta = total.Subtract(ByteSize.FromKiloBytes(10));
delta = delta - ByteSize.FromGigaBytes(100);
delta = delta.AddMegaBytes(-100);
// Multiple
var multiple = ByteSize.FromBytes(4) * ByteSize.FromBytes(2); // 8
// Divide
var divide = ByteSize.FromBytes(16) / ByteSize.FromBytes(8); // 2
You can create a ByteSize object from bits, bytes, kilobytes, megabytes, gigabytes, and terabytes.
new ByteSize(15); // Constructor takes in bits (long)
new ByteSize(1.5); // ... or bytes (double)
// Static Constructors
ByteSize.FromBits(10); // Same as constructor
ByteSize.FromBytes(1.5); // Same as constructor
// Decimal: 1 KB = 1000 B
ByteSize.FromKiloBytes(1.5);
ByteSize.FromMegaBytes(1.5);
ByteSize.FromGigaBytes(1.5);
ByteSize.FromTeraBytes(1.5);
// Binary: 1 KiB = 1024 B
ByteSize.FromKibiBytes(1.5);
ByteSize.FromMebiBytes(1.5);
ByteSize.FromGibiBytes(1.5);
ByteSize.FromTebiBytes(1.5);
A ByteSize object contains representations in:
bits, byteskilobytes, megabytes, gigabytes, and terabyteskibibytes, mebibytes, gibibytes, and tebibytesvar maxFileSize = ByteSize.FromKiloBytes(10);
maxFileSize.Bits; // 80000
maxFileSize.Bytes; // 10000
// Decimal
maxFileSize.KiloBytes; // 10
maxFileSize.MegaBytes; // 0.01
maxFileSize.GigaBytes; // 1E-05
maxFileSize.TeraBytes; // 1E-08
// Binary
maxFileSize.KibiBytes; // 9.765625
maxFileSize.MebiBytes; // 0.0095367431640625
maxFileSize.GibiBytes; // 9.31322574615479E-06
maxFileSize.TebiBytes; // 9.09494701772928E-09
A ByteSize object also contains four properties that represent the largest whole number symbol and value.
var maxFileSize = ByteSize.FromKiloBytes(10);
maxFileSize.LargestWholeNumberDecimalSymbol; // "KB"
maxFileSize.LargestWholeNumberDecimalValue; // 10
maxFileSize.LargestWholeNumberBinarySymbol; // "KiB"
maxFileSize.LargestWholeNumberBinaryValue; // 9.765625
By default a ByteSize object uses the decimal value for string representation.
All string operations are localized to use the number decimal separator of the culture set in Thread.CurrentThread.CurrentCulture.
ByteSize comes with a handy ToString method that uses the largest metric prefix whose value is greater than or equal to 1.
// By default the decimal values are used
ByteSize.FromBits(7).ToString(); // 7 b
ByteSize.FromBits(8).ToString(); // 1 B
ByteSize.FromKiloBytes(.5).ToString(); // 500 B
ByteSize.FromKiloBytes(999).ToString(); // 999 KB
ByteSize.FromKiloBytes(1000).ToString(); // 1 MB
ByteSize.FromGigabytes(.5).ToString(); // 500 MB
ByteSize.FromGigabytes(1000).ToString(); // 1 TB
// Binary
ByteSize.Parse("1.55 kb").ToString("kib"); // 1.51 kib
The ToString method accepts a single string parameter to format the output.
The formatter can contain the symbol of the value to display.
b, BKB, MB, GB, TBKiB, MiB, GiB, TiBThe formatter uses the built in double.ToString method.
The default number format is 0.## which rounds the number to two decimal
places and outputs only 0 if the value is 0.
You can include symbol and number formats.
var b = ByteSize.FromKiloBytes(10.505);
// Default number format is 0.##
b.ToString("KB"); // 10.52 KB
b.ToString("MB"); // .01 MB
b.ToString("b"); // 86057 b
// Default symbol is the largest metric prefix value >= 1
b.ToString("#.#"); // 10.5 KB
// All valid values of double.ToString(string format) are acceptable
b.ToString("0.0000"); // 10.5050 KB
b.ToString("000.00"); // 010.51 KB
// You can include number format and symbols
b.ToString("#.#### MB"); // .0103 MB
b.ToString("0.00 GB"); // 0 GB
b.ToString("#.## B"); // 10757.12 B
// ByteSize object of value 0
var zeroBytes = ByteSize.FromKiloBytes(0);
zeroBytes.ToString(); // 0 b
zeroBytes.ToString("0 kb"); // 0 kb
zeroBytes.ToString("0.## mb"); // 0 mb
ByteSize has a Parse and TryParse method similar to other base classes.
Like other TryParse methods, ByteSize.TryParse returns boolean
value indicating whether or not the parsing was successful. If the value is
parsed it is output to the out parameter supplied.
ByteSize output;
ByteSize.TryParse("1.5mb", out output);
ByteSize.TryParse("1.5mib", out output);
// Invalid
ByteSize.Parse("1.5 b"); // Can't have partial bits
// Valid
ByteSize.Parse("5b");
ByteSize.Parse("1.55B");
ByteSize.Parse("1.55KB");
ByteSize.Parse("1.55 kB "); // Spaces are trimmed
ByteSize.Parse("1.55 kb");
ByteSize.Parse("1.55 MB");
ByteSize.Parse("1.55 mB");
ByteSize.Parse("1.55 mb");
ByteSize.Parse("1.55 GB");
ByteSize.Parse("1.55 gB");
ByteSize.Parse("1.55 gib");
ByteSize.Parse("1.55 TiB");
ByteSize.Parse("1.55 tiB");
ByteSize.Parse("1.55 tib");
ByteSize.Parse("1,55 kib"); // de-DE culture
Omar Khudeira (http://omar.io)
Copyright (c) 2013-2022 Omar Khudeira. All rights reserved.
Released under MIT License (see LICENSE file).
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp1.0 netcoreapp1.0 was computed. netcoreapp1.1 netcoreapp1.1 was computed. netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard1.0 netstandard1.0 is compatible. netstandard1.1 netstandard1.1 was computed. netstandard1.2 netstandard1.2 was computed. netstandard1.3 netstandard1.3 was computed. netstandard1.4 netstandard1.4 was computed. netstandard1.5 netstandard1.5 was computed. netstandard1.6 netstandard1.6 was computed. netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 was computed. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen30 tizen30 was computed. tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Universal Windows Platform | uap uap was computed. uap10.0 uap10.0 was computed. |
| Windows Phone | wp8 wp8 was computed. wp81 wp81 was computed. wpa81 wpa81 was computed. |
| Windows Store | netcore netcore was computed. netcore45 netcore45 was computed. netcore451 netcore451 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on ByteSize:
| Package | Downloads |
|---|---|
|
FenixAlliance.ACL.Dependencies
Application Component for the Alliance Business Suite. |
|
|
Relativity.Transfer.SDK
Relativity Transfer SDK allows performing high-throughput transfers of files from and to Relativity environment. |
|
|
Xinghe.Utility
XH基础库(内部使用) |
|
|
Cathedral
Various small tools and utility classes |
|
|
Zen.Base
A fluid, seamless middleware. |
Showing the top 20 popular GitHub repositories that depend on ByteSize:
| Repository | Stars |
|---|---|
|
files-community/Files
A modern file manager that helps users organize their files and folders.
|
|
|
PixiEditor/PixiEditor
PixiEditor is a Universal Editor for all your 2D needs
|
|
| beeradmoore/dlss-swapper | |
|
jenius-apps/ambie
An app that uses white noise, nature sounds, and focus features to boost your productivity.
|
|
|
Bluegrams/Vividl
Modern Windows GUI for youtube-dl/ yt-dlp. Download videos from hundreds of websites.
|
|
|
xoofx/ultra
An advanced profiler for .NET Applications on Windows
|
|
|
WOA-Project/WOA-Deployer-Rpi
WOA Deployer for Raspberry Pi
|
|
|
aloneguid/parquet-dotnet
Fully managed Apache Parquet implementation
|
|
|
WOA-Project/WoA-Installer-Rpi
This repository was deprecated, use:
|
|
|
Reaparr/Reaparr
Plex downloader that brings content from any server to yours!
|
|
|
DataDog/dd-trace-dotnet
.NET Client Library for Datadog APM
|
|
|
Dynatrace/superdump
A service for automated crash-dump analysis
|
|
|
LunaMultiplayer/LunaMultiplayer
Multiplayer mod for Kerbal Space Program (KSP)
|
|
|
WOA-Project/WOA-Deployer-Lumia
Making your Lumias great again!
|
|
|
pearlxcore/PS4-PKG-Tool
Manage and perform various operations on PS4 PKG.
|
|
|
davidxuang/MusicDecrypto
Cross-platform solution for music de-obfuscation
|
|
| LANCommander/LANCommander | |
|
Maxstupo/ydl-ui
A UI for the command-line video downloader "youtube-dl"
|
|
|
Mongo2Go/Mongo2Go
Mongo2Go - MongoDB for .NET integration tests
|
|
|
sungaila/PDFtoImage
A .NET library to render PDF files into images.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.2 | 3,684,374 | 1/14/2024 |
| 2.1.1 | 3,621,895 | 11/6/2021 |
| 2.1.0 | 11,139,460 | 9/3/2021 |
| 2.0.0 | 1,307,562 | 1/14/2020 |
| 1.3.0 | 722,430 | 1/22/2017 |
| 1.2.5 | 2,902 | 1/21/2017 |
| 1.2.4 | 5,537 | 11/25/2016 |
| 1.2.3 | 12,803 | 11/10/2016 |
| 1.2.2 | 3,734 | 10/28/2016 |
| 1.2.1 | 29,838 | 7/23/2016 |
| 1.2.0 | 4,780 | 6/18/2016 |
| 1.1.3 | 3,656 | 6/18/2016 |
- Add strong name support
View all release notes at https://github.com/omar/ByteSize/blob/master/CHANGELOG.md.