SquirrelSizer 3.0.0
dotnet add package SquirrelSizer --version 3.0.0
NuGet\Install-Package SquirrelSizer -Version 3.0.0
<PackageReference Include="SquirrelSizer" Version="3.0.0" />
<PackageVersion Include="SquirrelSizer" Version="3.0.0" />
<PackageReference Include="SquirrelSizer" />
paket add SquirrelSizer --version 3.0.0
#r "nuget: SquirrelSizer, 3.0.0"
#:package SquirrelSizer@3.0.0
#addin nuget:?package=SquirrelSizer&version=3.0.0
#tool nuget:?package=SquirrelSizer&version=3.0.0
Squirrel-Sizer
A simple file sizing library. It turns byte counts, files and folders into readable sizes such as 1.5 GB, and can parse them back.
Get it from NuGet: https://www.nuget.org/packages/SquirrelSizer/
dotnet add package SquirrelSizer
It targets .NET 10.
Upgrading from 2.x? See the migration notes in CHANGELOG.md.
Give it a number and it gives you a string with the size
using SquirrelUtils;
long number = 1234500;
Sizer.Suffix(number);
// "1 MB"
or
long number = 1234500;
Sizer.SuffixName(number);
// "1 Megabyte"
Both take long or ulong. A negative number throws ArgumentOutOfRangeException.
Decimal places
long number = 1234500;
Sizer.Suffix(number, 2);
// "1.18 MB"
Sizer.SuffixName(number, 4);
// "1.1773 Megabytes"
You always get exactly the number of decimal places you ask for, so Sizer.Suffix(1048576, 2) is "1.00 MB". Halves round away from zero.
Just the unit
long number = 1234500;
Sizer.Suffix(number, includeNumber: false);
// "MB"
Sizer.SuffixName(number, includeNumber: false);
// "Megabytes"
Files
Sizer.Suffix("C:\\Path\\To\\File\\file.txt");
Sizer.SuffixName("C:\\Path\\To\\File\\file.txt");
// or as extension methods
new FileInfo("C:\\Path\\To\\File\\file.txt").Suffix();
new FileInfo("C:\\Path\\To\\File\\file.txt").SuffixName();
A missing file throws FileNotFoundException. If a missing file is expected, use the Try versions:
if (Sizer.TrySuffix("C:\\Path\\To\\File\\file.txt", out string size)) {
Console.WriteLine(size);
}
Several files added together
List<string> files = new List<string>() {
"C:\\Path\\To\\File\\file1.txt",
"C:\\Path\\To\\File\\file2.txt",
"C:\\Path\\To\\File\\file3.txt"
};
Sizer.AllSuffix(files);
Sizer.AllSuffixName(files);
// any sequence of paths or FileInfo works
Sizer.AllSuffix(Directory.EnumerateFiles("C:\\Path\\To\\Folder"));
Folders
new DirectoryInfo("C:\\Path\\To\\Folder").Suffix();
new DirectoryInfo("C:\\Path\\To\\Folder").SuffixName(2);
// top level only
new DirectoryInfo("C:\\Path\\To\\Folder").Suffix(recursive: false);
Subfolders the process cannot read are skipped. Symbolic links and junctions to folders are not followed.
Units: 1024 or 1000
By default sizes are divided by 1024 and labelled KB, MB, GB, the same as Windows Explorer. Pass units: to change that.
long number = 1234500;
Sizer.Suffix(number, 2); // "1.18 MB"
Sizer.Suffix(number, 2, units: SizeUnits.BinaryIec); // "1.18 MiB"
Sizer.Suffix(number, 2, units: SizeUnits.Decimal); // "1.23 MB"
SizeUnits |
Divides by | Labels |
|---|---|---|
Binary (default) |
1024 | KB, MB, GB / Kilobytes |
BinaryIec |
1024 | KiB, MiB, GiB / Kibibytes |
Decimal |
1000 | KB, MB, GB / Kilobytes |
Culture
The number is formatted with the current culture, so a German machine shows 1,18 MB. Pass provider: to choose.
Sizer.Suffix(1234500, 2, provider: CultureInfo.InvariantCulture);
// always "1.18 MB"
Text back to bytes
Sizer.Parse("1.5 GB"); // 1610612736
Sizer.Parse("10MB"); // 10485760
Sizer.Parse("3 Kilobytes"); // 3072
Sizer.Parse("1 MB", units: SizeUnits.Decimal); // 1000000
if (Sizer.TryParse(userInput, out long bytes)) {
// ...
}
Unit names are matched without regard to case. A number with no unit is read as bytes. KiB, MiB and the other IEC units are always 1024-based. Both methods also accept a ReadOnlySpan<char>.
Security notes
- Paths are used exactly as given. The library does not restrict them to any folder, and it resolves relative paths and
..like any other .NET file API. - If a path comes from an untrusted source (a query string, a form field, an API parameter), validate or canonicalise it first. Otherwise the result, or the exception, tells that user whether a file exists and how big it is.
Building and testing
dotnet build
dotnet test
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.