File.TypeChecker
4.3.0
dotnet add package File.TypeChecker --version 4.3.0
NuGet\Install-Package File.TypeChecker -Version 4.3.0
<PackageReference Include="File.TypeChecker" Version="4.3.0" />
<PackageVersion Include="File.TypeChecker" Version="4.3.0" />
<PackageReference Include="File.TypeChecker" />
paket add File.TypeChecker --version 4.3.0
#r "nuget: File.TypeChecker, 4.3.0"
#:package File.TypeChecker@4.3.0
#addin nuget:?package=File.TypeChecker&version=4.3.0
#tool nuget:?package=File.TypeChecker&version=4.3.0
π‘οΈ FileTypeChecker
<div align="center">
Secure file type validation for .NET applications using magic number detection
</div>
β¨ Overview
FileTypeChecker is a powerful .NET library that provides reliable file type identification using magic number detection. Unlike traditional filename extension-based validation, this library analyzes the actual file content to determine the true file type, protecting your applications from malicious files and ensuring robust security.
π Table of Contents
- π Quick Start
- π‘ Why Use FileTypeChecker?
- βοΈ How It Works
- π¦ Installation
- π§ Usage Examples
- π Supported File Types
- π Web Applications
- π€ Contributing
- π Support the Project
- π License
π Quick Start
using (var fileStream = File.OpenRead("suspicious-file.exe"))
{
// Check if file type can be identified
if (FileTypeValidator.IsTypeRecognizable(fileStream))
{
// Get the actual file type
IFileType fileType = FileTypeValidator.GetFileType(fileStream);
Console.WriteLine($"File type: {fileType.Name} ({fileType.Extension})");
// Check specific type
bool isImage = fileStream.IsImage();
bool isPdf = fileStream.Is<PortableDocumentFormat>();
}
}
π‘ Why Use FileTypeChecker?
π― The Problem
Traditional file validation relies on file extensions, which can be easily manipulated:
- A malicious executable can be renamed to
.jpg
- Untrusted files can bypass basic extension checks
- The
FileSystemInfo.Extension
property only reads the filename
β The Solution
FileTypeChecker analyzes the actual file content using magic numbers:
- Reliable: Identifies files by their binary signature, not filename
- Secure: Prevents malicious files from masquerading as safe formats
- Comprehensive: Supports 30+ file types with extensible architecture
- Fast: Minimal performance overhead with efficient binary analysis
βοΈ How It Works
FileTypeChecker uses magic numbers (binary signatures) to identify file types. These are specific byte sequences found at the beginning of files that uniquely identify the format.
π Magic Number Examples
PDF: 25 50 44 46 (%PDF)
PNG: 89 50 4E 47 (β°PNG)
JPEG: FF D8 FF (ΓΏΓΓΏ)
ZIP: 50 4B 03 04 (PK..)
This method provides reliable identification regardless of file extension, offering better security guarantees than filename-based validation.
π Learn more about Magic Numbers on Wikipedia
π¦ Installation
Package Manager
Install-Package File.TypeChecker
.NET CLI
dotnet add package File.TypeChecker
PackageReference
<PackageReference Include="File.TypeChecker" Version="4.2.0" />
Requirements: .NET Standard 2.0+
π§ Usage Examples
Basic File Type Detection
using FileTypeChecker;
using (var fileStream = File.OpenRead("document.pdf"))
{
// Check if file type is recognizable
if (FileTypeValidator.IsTypeRecognizable(fileStream))
{
// Get file type information
IFileType fileType = FileTypeValidator.GetFileType(fileStream);
Console.WriteLine($"Type: {fileType.Name}");
Console.WriteLine($"Extension: {fileType.Extension}");
}
}
Category-Based Validation
using (var fileStream = File.OpenRead("image.jpg"))
{
// Check by category
bool isImage = fileStream.IsImage();
bool isDocument = fileStream.IsDocument();
bool isArchive = fileStream.IsArchive();
// Check specific type
bool isPng = fileStream.Is<PortableNetworkGraphic>();
bool isJpeg = fileStream.Is<JointPhotographicExpertsGroup>();
}
File Upload Validation
public bool ValidateUploadedFile(IFormFile file)
{
using (var stream = file.OpenReadStream())
{
// Verify file is actually an image (regardless of file extension)
if (!stream.IsImage())
{
throw new InvalidOperationException("Only image files are allowed");
}
// Additional validation for specific formats
var fileType = FileTypeValidator.GetFileType(stream);
var allowedTypes = new[] { "PNG", "JPEG", "BMP" };
return allowedTypes.Contains(fileType.Name);
}
}
Custom File Type Registration
// Register your own file type
public class MyCustomType : FileType
{
public override string Name => "My Custom Format";
public override string Extension => "mycustom";
public override string MimeType => "application/x-mycustom";
public override bool IsMatch(byte[] signature, Stream stream)
{
return signature.Length >= 4 &&
signature[0] == 0x4D && signature[1] == 0x59 &&
signature[2] == 0x43 && signature[3] == 0x54;
}
}
// Use it
FileTypeValidator.RegisterType<MyCustomType>();
π More examples available in our Wiki
π Supported File Types
FileTypeChecker supports 30+ file formats across multiple categories:
πΌοΈ Images
- PNG - Portable Network Graphics
- JPEG - Joint Photographic Experts Group
- GIF - Graphics Interchange Format (87a/89a)
- BMP - Bitmap Image File
- TIFF - Tagged Image File Format
- WebP - WebP Image Format
- ICO - Icon File
- PSD - Photoshop Document
- HEIC - High Efficiency Image Container
π Documents
- PDF - Portable Document Format
- DOC/DOCX - Microsoft Word Documents
- XLS/XLSX - Microsoft Excel Spreadsheets
- HTML - HyperText Markup Language
- XML - Extensible Markup Language
ποΈ Archives
- ZIP - ZIP Archive
- RAR - RAR Archive
- 7Z - 7-Zip Archive
- TAR - TAR Archive
- GZIP - GNU Zip
- BZIP2 - BZIP2 Compressed File
π΅ Audio/Video
- MP3 - MPEG Audio Layer 3
- MP4 - MPEG-4 Video
- M4V - iTunes Video
- AVI - Audio Video Interleave
- WAV - Windows Audio
π» Executables
- EXE - Windows Executable
- ELF - Executable and Linkable Format
β Extensible
Add your own custom file types by implementing the IFileType
interface.
π Complete list available in our Wiki
π Web Applications
For ASP.NET Core applications, check out FileTypeChecker.Web - a companion package with validation attributes for IFormFile
:
public class UploadModel
{
[AllowedFileTypes(FileType.Jpeg, FileType.Png)]
[MaxFileSize(5 * 1024 * 1024)] // 5MB
public IFormFile ProfileImage { get; set; }
}
Features
- β Pre-built validation attributes
- β Model binding integration
- β Automatic error messages
- β Easy file upload validation
π€ Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Development
git clone https://github.com/AJMitev/FileTypeChecker.git
cd FileTypeChecker
dotnet restore
dotnet build
dotnet test
π Support the Project
If this library helps you, consider supporting its development:
- β Star the repository and share it with others
- β Buy me a coffee for continued development
- π₯ Become a member for direct access to maintainers
<a href="https://www.buymeacoffee.com/ajmitev" target="_blank"> <img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" height="41" width="174"> </a>
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Credits
- Based on mjolka's Stack Overflow answer: Guessing a file type based on its content
- Inspired by 0xbrock's FileTypeChecker
- Completely rewritten with object-oriented design, fluent API, and extensibility
<div align="center"> <strong>Made with β€οΈ by <a href="https://github.com/AJMitev">Aleksandar J. Mitev</a></strong> </div>
Product | Versions 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. 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 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | 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 | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages (13)
Showing the top 5 NuGet packages that depend on File.TypeChecker:
Package | Downloads |
---|---|
Mii.Rinjani.Gateway.Commons
Package Description |
|
DeploySoftware.LaunchPad.Core
Shared code for LaunchPad framework. |
|
File.TypeChecker.Web
Don't let users to inject you an invalid file! FileTypeChecker is a easy to use library that allows you to read file and recognize its type. This will help you to validate all files that is provided by external sources. |
|
SAPI
Simple API server written in C# |
|
File.TypeChecker.Async
Don't let users to inject you an invalid file! FileTypeChecker is a easy to use library that allows you to read file and recognize its type. This will help you to validate all files that is provided by external sources. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
4.3.0 | 811 | 9/26/2025 |
4.2.0 | 21,295 | 8/15/2025 |
4.1.1 | 619,622 | 4/27/2024 |
4.1.0 | 411,101 | 4/6/2024 |
4.0.0 | 868,256 | 5/2/2023 |
3.0.0 | 625,920 | 12/16/2021 |
2.1.0 | 6,400 | 11/16/2021 |
2.0.3 | 1,589 | 11/11/2021 |
1.5.3 | 24,518 | 10/29/2021 |
1.5.2 | 32,758 | 8/2/2021 |
1.5.1 | 47,004 | 4/30/2021 |
1.4.0 | 64,584 | 11/24/2020 |
1.3.3 | 23,628 | 10/20/2020 |
1.3.2 | 5,707 | 10/4/2020 |
1.3.1 | 126,612 | 5/15/2020 |
1.3.0 | 9,193 | 5/7/2020 |
1.2.0 | 2,363 | 3/14/2020 |
1.1.0 | 1,661 | 3/7/2020 |
1.0.4 | 2,155 | 10/5/2019 |
1.0.3 | 1,887 | 10/5/2019 |
1.0.1 | 1,832 | 10/5/2019 |
1.0.0 | 11,070 | 10/4/2019 |