Vali-FileSize 1.0.0

dotnet add package Vali-FileSize --version 1.0.0
                    
NuGet\Install-Package Vali-FileSize -Version 1.0.0
                    
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="Vali-FileSize" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-FileSize" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-FileSize" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vali-FileSize --version 1.0.0
                    
#r "nuget: Vali-FileSize, 1.0.0"
                    
#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.
#:package Vali-FileSize@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Vali-FileSize&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-FileSize&version=1.0.0
                    
Install as a Cake Tool

Vali-FileSize - File Size Conversion and Formatting for .NET

Introduction πŸš€

Welcome to Vali-FileSize , a lightweight .NET library designed to simplify file size conversions and formatting. Whether you're working with bytes, kilobytes, megabytes, gigabytes, terabytes, or petabytes, Vali-FileSize provides a fluent and intuitive API to convert sizes, format them into human-readable strings, and automatically detect the most suitable unit. Perfect for applications requiring precise file size handling, this library integrates seamlessly into any .NET project.

Installation πŸ“¦

To add Vali-FileSize to your .NET project, install it via NuGet with the following command:

dotnet add package Vali-FileSize

Ensure your project targets a compatible .NET version (e.g., .NET Standard 2.0 or later). Vali-FileSize is lightweight and has minimal dependencies, making it an easy addition to your application.

Usage πŸ› οΈ

Vali-FileSize focuses on converting file sizes between units and formatting them for display. The library provides a simple class, ValiFileSize , with methods to perform conversions, format sizes with cultural support, and determine the best unit for a given size.

Basic Example

Here’s how you can convert and format a file size:

using Shared.Converters;
using Shared.Enums.Data;

var converter = new ValiFileSize();

// Convert 1.5 terabytes to bytes
double sizeInBytes = converter.Convert(1.5, FileSizeUnit.Terabytes, FileSizeUnit.Bytes);
Console.WriteLine(sizeInBytes); // Outputs: 1,610,612,736,000

// Format a size in megabytes
string formattedSize = converter.FormatSize(123.456, FileSizeUnit.Megabytes);
Console.WriteLine(formattedSize); // Outputs: "123.46 MB"

Key Methods πŸ“

Vali-FileSize offers a straightforward API for file size management. Below are the key methods provided by the ValiFileSize class:

Convert πŸ—οΈ

Converts a file size from one unit to another with precision:

var converter = new ValiFileSize();

// Convert 2.5 gigabytes to kilobytes
double sizeInKilobytes = converter.Convert(2.5, FileSizeUnit.Gigabytes, FileSizeUnit.Kilobytes);
Console.WriteLine(sizeInKilobytes); // Outputs: 2,621,440

FormatSize 🎨

Formats a size into a human-readable string with customizable decimal places and cultural formatting:

var converter = new ValiFileSize();

// Format 1,234,567 bytes as megabytes
string formatted = converter.FormatSize(1234567, FileSizeUnit.Megabytes, decimalPlaces: 3);
Console.WriteLine(formatted); // Outputs: "1.177 MB"

GetBestUnit πŸ”

Automatically determines the most appropriate unit for a given size in bytes:

var converter = new ValiFileSize();

// Find the best unit for 1,234,567,890 bytes
var (size, unit) = converter.GetBestUnit(1234567890);
string bestFormat = converter.FormatSize(size, unit);
Console.WriteLine(bestFormat); // Outputs: "1.15 GB"

Working with Advanced Features 🧩

Vali-FileSize supports advanced use cases like cultural formatting and precise conversions:

Cultural Formatting

Format sizes according to specific cultures:

using System.Globalization;

var converter = new ValiFileSize();
var germanCulture = new CultureInfo("de-DE");

string formatted = converter.FormatSize(1234.567, FileSizeUnit.Kilobytes, 2, germanCulture);
Console.WriteLine(formatted); // Outputs: "1234,57 KB" (uses comma as decimal separator)

Combining Features

Convert, detect the best unit, and format in one flow:

var converter = new ValiFileSize();
double bytes = converter.Convert(5.75, FileSizeUnit.Terabytes, FileSizeUnit.Bytes);

var (size, unit) = converter.GetBestUnit(bytes);
string result = converter.FormatSize(size, unit);

Console.WriteLine(result); // Outputs: "5.75 TB"

Comparison: Without vs. With Vali-FileSize βš–οΈ

Without Vali-FileSize (Manual Conversion)

Manually handling file size conversions can be tedious and error-prone:

double bytes = 2.5 * 1024 * 1024 * 1024; // GB to bytes
double kilobytes = bytes / 1024;
Console.WriteLine($"{kilobytes:F2} KB"); // Outputs: "2621440.00 KB"

With Vali-FileSize (Simplified Conversion)

Vali-FileSize streamlines the process with a clean API:

var converter = new ValiFileSize();
double kilobytes = converter.Convert(2.5, FileSizeUnit.Gigabytes, FileSizeUnit.Kilobytes);
string formatted = converter.FormatSize(kilobytes, FileSizeUnit.Kilobytes);
Console.WriteLine(formatted); // Outputs: "2621440.00 KB"

Features and Enhancements 🌟

Recent Updates

  • Initial release (v1.0.0) with support for conversions across bytes, KB, MB, GB, TB, and PB.
  • Added FormatSize method with customizable decimal precision and cultural number formatting.
  • Introduced GetBestUnit for automatic unit selection, improving usability.
  • Ensured robust validation with negative size checks and comprehensive exception handling.

Planned Features

  • Support for additional units like exabytes (EB) and beyond.
  • Enhanced formatting options, such as binary prefixes (KiB, MiB, etc.) for IEC standards.

Follow the project on GitHub for updates on new features and improvements!

Donations πŸ’–

If you find Vali-FileSize useful and would like to support its development, consider making a donation:

Your contributions help keep this project alive and improve its development! πŸš€

License πŸ“œ

This project is licensed under the Apache License 2.0.

Contributions 🀝

Feel free to open issues and submit pull requests to improve this library!

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.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.

Version Downloads Last Updated
1.0.0 182 4/10/2025

Vali-FileSize v1.0.0 introduces a lightweight and efficient solution for file size management in .NET applications:
           - Support for conversions across multiple units (bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes), providing precise and flexible size handling.
           - Addition of human-readable formatting with customizable decimal precision and cultural support, making file sizes easy to display and understand.
           - Introduction of automatic unit detection, allowing developers to effortlessly determine the most appropriate unit for any given size, enhancing usability in diverse scenarios.
           Designed for seamless integration with .NET Standard 2.0 and above, Vali-FileSize ensures compatibility and reliability for modern .NET development.