A DataSize value type for .NET — and DiskScope, a disk-usage explorer built on it. DataSize
parses and formats human byte strings in both binary (KiB = 1024) and decimal (KB = 1000)
units; DiskScope scans a directory tree and shows where the space goes — the biggest folders, file
types and files — with every size rendered by DataSize.
Two things in one repo:
ByteFmt— the dependency-free, NuGet-packable value type (parse, format, compare, arithmetic).- DiskScope — a scan engine (
ByteFmt.DiskScan) plus an ASP.NET Core 8 web dashboard and REST API (ByteFmt.Web).
dotnet run --project src/ByteFmt.Web # http://localhost:5000
# then enter a directory path in the UI, or:
curl "http://localhost:5000/api/scan?path=/var/log&top=10"With Docker (mount the directory you want to scan read-only):
docker build -t diskscope .
docker run -p 8080:8080 -v "$PWD:/data:ro" diskscope # then scan /data in the UIA scan reports the total size, file and directory counts, the biggest immediate subfolders (sized recursively), a breakdown by file extension, and the largest individual files. Inaccessible files and folders are skipped and counted rather than aborting the scan. The JSON API:
Note: the scanner reads the server's own filesystem. Bind the app to localhost (or run it in a container with a read-only mount) — don't expose it to untrusted networks.
using ByteFmt.DiskScan;
var result = new DirectoryScanner().Scan("/var/log", new ScanOptions { Top = 10 });
Console.WriteLine($"{result.TotalSize} across {result.FileCount} files");
foreach (var folder in result.TopFolders)
Console.WriteLine($" {folder.Name,-20} {folder.Size} ({folder.FileCount} files)");using ByteFmt;
new DataSize(1_536).ToString(); // "1.5 KiB"
new DataSize(2_500_000_000).Humanize(UnitStandard.Decimal); // "2.5 GB"
DataSize.Parse("1.5 GiB").Bytes; // 1610612736
DataSize.Parse("900 MB").Bytes; // 900000000
DataSize.Parse("1,024 KB").Bytes; // 1024000
var total = DataSize.FromMebibytes(4) + DataSize.FromKibibytes(512); // 4.5 MiB
DataSize.FromMebibytes(1).FractionOf(DataSize.FromMebibytes(4)); // 0.25- Parse human strings:
"2048","512 B","1.5 GiB","900 MB","1,024 KB","1K". Binary (KiB) and decimal (KB) suffixes are distinguished; a bare letter (1K) is treated as binary. Parsing is case-insensitive and tolerates thousands separators and spaces. - Format with
Humanize(standard, decimals): the largest unit that keeps the value ≥ 1 is chosen, and sub-unit values are shown as whole bytes.ToString()uses binary units. - A proper value type: immutable, non-negative, with
==,<,>,CompareTo,+,-,* int, equality and hashing all keyed on the byte count, soDataSizesorts and compares naturally. FractionOf(whole)for share/percentage breakdowns, plusFromBytes/FromKibibytes/… factories.
Invalid input ("", "abc", "-5 KB", an unknown suffix) makes Parse throw FormatException and
TryParse return false. A negative size throws ArgumentOutOfRangeException.
- Bytes are the single source of truth. A
DataSizestores alongbyte count; every unit value is derived, so there is no rounding drift between representations and equality is exact. - Binary vs decimal is a choice at the edges. The stored value is unit-agnostic; the unit standard is chosen when formatting and inferred from the suffix when parsing.
- Overflow is checked. Parsing and arithmetic use
checkedarithmetic, so an overflowing value fails loudly (or, forTryParse, returnsfalse) rather than wrapping.
dotnet testSixty-two tests: the DataSize value type (binary/decimal formatting at the unit boundaries,
precision, every parse form, round-tripping, operators, equality/hashing, FractionOf, rejected
input), the DirectoryScanner (totals, recursive folder sizes, extension aggregation, largest files,
the Top limit, empty/missing/blank-path handling), and the web API (scan totals, unit switching,
error cases, and the rendered pages) via WebApplicationFactory. CI runs on Linux and Windows and
packs the NuGet package.
src/
├── ByteFmt/ the DataSize value type (NuGet package)
│ ├── DataSize.cs parse, format, operators, FractionOf
│ └── UnitStandard.cs binary/decimal units and suffix resolution
├── ByteFmt.DiskScan/ the directory scan engine (DirectoryScanner, ScanResult)
└── ByteFmt.Web/ the ASP.NET Core dashboard + REST API
tests/
├── ByteFmt.Tests/ xUnit tests for the value type
├── ByteFmt.DiskScan.Tests/ scanner tests over a temp tree
└── ByteFmt.Web.Tests/ WebApplicationFactory integration tests
MIT
