NeoSmart.PrettySize 3.1.1

Prefix Reserved
dotnet add package NeoSmart.PrettySize --version 3.1.1                
NuGet\Install-Package NeoSmart.PrettySize -Version 3.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NeoSmart.PrettySize" Version="3.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NeoSmart.PrettySize --version 3.1.1                
#r "nuget: NeoSmart.PrettySize, 3.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NeoSmart.PrettySize as a Cake Addin
#addin nuget:?package=NeoSmart.PrettySize&version=3.1.1

// Install NeoSmart.PrettySize as a Cake Tool
#tool nuget:?package=NeoSmart.PrettySize&version=3.1.1                

PrettySize, a .NET library for dealing with and formatting file sizes

PrettySize is a .NET (2.0+ with full .NET Core/.NET 6 support) library for dealing with and formatting/pretty-printing sizes of all kind. It was originally designed to make automatically formatting a file size (in terms of converting to the correct unit and choosing just the right amount of decimal precision) for pretty printing for human consumption a breeze, and has since evolved to include additionally functionality.

Installation

PrettySize is published as a NuGet package, meaning installation is as simple as looking up "PrettySize" in the Visual Studio package manager or by typing the following in the Visual Studio "Package Manager Console":

Install-Package NeoSmart.PrettySize

Use

The PrettySize package exposes the entirety of its surface area via the PrettySize struct/class under the NeoSmart.PrettySize namespace. PrettySize is a readonly struct wrapper around a file size and exposes strongly-typed size-related operations via that interface.

Creating a PrettySize instance can be done directly (given a raw file size in terms of bytes) or via any of the static helper functions/constructors that provide for unit-based construction. Both base-10 (KB, MB, etc) and base-2 (KiB, MiB, etc) units are supported via the same interface:

using NeoSmart.PrettySize;

public void Main() {
    // Initializing directly via the default constructor:
    var size = new PrettySize(200);
    Console.WriteLine($"Size: {size}"); // Prints "Size: 200 bytes"

    // Initializing via a unit-based helper function:
    var size1 = PrettySize.KiB(28);
    var size2 = PrettySize.Bytes(14336);
    var sum = size1 + size2;
    Console.WriteLine($"The total size is {sum}"); // Prints "The total size is 42.00 KiB"
}

The benefits of using a strongly-typed object instead of passing around long or ulong file sizes include (as shown above) the ability to add sizes with different units, being prevented from mixing up types and performing mathematical operations that make no sense (you can safely add/subtract two PrettySize values or you can multiply/divide a PrettySize value and a scalar long value, but you can't do something like accidentally multiply two file sizes together), and having something that you can always just print or convert to a string without worrying about formatting and readability.

For more control over how sizes are formatted as text, you can use PrettySize.Format() method, which lets you specify the units to be used (base-2 or base-10) and how unit names are spelled out (abbreviated, unabridged, lowercase, etc):

// using NeoSmart.PrettySize
var size = PrettySize.Bytes(2048);

var formatted = size.Format(UnitBase.Base2, UnitStyle.Full);
Console.WriteLine(formatted); // Prints "2.00 Kebibytes"

var formatted2 = size.Format(UnitBase.Base10, UnitStyle.FullLower);
Console.WriteLine(formatted2); // Prints "2.05 kilobytes"

Use with scalar size values

The PrettySize project also includes some constants and static methods that can be used to create and format scalar file sizes (as long values expressing the file size in bytes). Their usage is discouraged and is currently included only for legacy compatibility:

// using NeoSmart.PrettySize
var untypedSize = 1 * EXABYTE;
var formatted = PrettySize.Format(untypedSize, UnitBase.Base10);
Console.WriteLine(formatted); // Prints "1.00 EB"

License and Credits

PrettySize was developed by Mahmoud Al-Qudsi of NeoSmart Technologies and is released under the terms of the MIT public license. The name "PrettySize" is copyright NeoSmart Technologies 2017 and may not be used without permission (fair use is excluded).

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 is compatible. 
.NET Framework net20 is compatible.  net35 was computed.  net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 2.0

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 1.3

  • .NETStandard 2.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on NeoSmart.PrettySize:

Package Downloads
Netops-Transfer-Core-00002

Package Description

Netops-Transfer-Core-00001

Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on NeoSmart.PrettySize:

Repository Stars
Richasy/FantasyCopilot
A new-age AI desktop tool
Richasy/RichasyAssistant
个人助理
Version Downloads Last updated
3.1.1 21,565 6/27/2022
3.1.0 453 6/26/2022
3.0.0 470 6/18/2022
2.1.0 4,568 6/11/2017
2.0.0 1,031 6/11/2017
1.0.0 1,083 6/10/2017