FileMagicNet 1.0.0

dotnet add package FileMagicNet --version 1.0.0
                    
NuGet\Install-Package FileMagicNet -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="FileMagicNet" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FileMagicNet" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FileMagicNet" />
                    
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 FileMagicNet --version 1.0.0
                    
#r "nuget: FileMagicNet, 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 FileMagicNet@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=FileMagicNet&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FileMagicNet&version=1.0.0
                    
Install as a Cake Tool

FileMagicNet

A modern, actively maintained .NET library for file type detection using magic bytes (file signatures). Never trust a file extension — validate the actual content.

Features

  • ✅ 60+ built-in signatures across images, documents, archives, media, executables, and fonts
  • ✅ Async-first API (ValidateAsync)
  • ✅ Works with Stream, byte[], or file path
  • ✅ Returns format name, MIME type, and file extension
  • ✅ Offset-aware matching (e.g. MP4, HEIC)
  • ✅ Custom signature registration
  • ✅ No external dependencies
  • ✅ Targets .NET 10

Installation

dotnet add package FileMagicNet

Quick Start

using FileMagicNet;

// From file path
var result = MagicBytesValidator.DetectFrom("upload.png");
Console.WriteLine(result.Format);    // PNG
Console.WriteLine(result.MimeType);  // image/png
Console.WriteLine(result.Extension); // .png
Console.WriteLine(result.IsMatch);   // true

// From stream (e.g. ASP.NET Core IFormFile)
using var stream = formFile.OpenReadStream();
var result = await MagicBytesValidator.DetectFromAsync(stream);

// Guard check — ensure upload is actually a PDF
bool isPdf = validator.IsMatch(stream, "application/pdf");

Validate Against Expected Type

var validator = new MagicBytesValidator();

// Quick guard
bool isValidImage = validator.IsMatch(stream, "image/png");

// Full result
var result = validator.Validate(stream);
if (!result.IsMatch)
{
    return BadRequest("Unknown file type.");
}

if (result.MimeType != "image/jpeg" && result.MimeType != "image/png")
{
    return BadRequest("Only JPEG and PNG are allowed.");
}

Register Custom Signatures

var registry = new SignatureRegistry(); // starts with all built-in signatures
registry.Register(new FileSignature(
    name:       "MyFormat",
    mimeType:   "application/x-myformat",
    extension:  ".myf",
    magicBytes: new byte[] { 0x4D, 0x59, 0x46 }
));

var validator = new MagicBytesValidator(registry);

Custom signatures are given priority over built-in ones.

Supported Formats

Images

PNG, JPEG, GIF, BMP, TIFF, ICO, HEIC, AVIF, WebP

Documents

PDF, DOCX, XLSX, PPTX, DOC, XLS, PPT, ODT, ODS, ODP, RTF, EPUB

Archives

ZIP, RAR, 7-Zip, GZIP, BZIP2, XZ, ZSTD, LZ4, TAR, ISO, CAB

Audio

MP3, WAV, FLAC, OGG, AAC, AIFF, M4A, MIDI, WMA

Video

MP4, MOV, AVI, MKV, WebM, FLV, WMV, MPEG, 3GP

Executables

ELF, PE/EXE, Mach-O, DEX, JVM Class, WASM

Fonts

TTF, OTF, WOFF, WOFF2, EOT

Multiple Matches

When a file matches multiple signatures (e.g. ZIP-based formats like DOCX, XLSX, PPTX), all matches are returned:

var result = validator.Validate(docxBytes);
Console.WriteLine(result.Format);           // Primary match
Console.WriteLine(result.AllMatches.Count); // All matching signatures

Note: For ZIP-based Office formats (DOCX/XLSX/PPTX), inspect the archive contents (look for [Content_Types].xml) to disambiguate definitively.

ASP.NET Core Integration

// Program.cs
builder.Services.AddSingleton<MagicBytesValidator>();

// Controller
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
    await using var stream = file.OpenReadStream();
    var result = await _validator.ValidateAsync(stream);

    if (!result.IsMatch || result.MimeType != "image/jpeg")
        return BadRequest("Only JPEG images are accepted.");

    // proceed with safe upload...
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 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 was computed.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.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 37 6/3/2026