Detect MIME types from file signatures (magic bytes) and file extensions — covers 200+ common file types.
dotnet add package Philiprehberger.MimeDetectDetect MIME types from file signatures (magic bytes) and file extensions — covers 200+ common file types.
dotnet add package Philiprehberger.MimeDetect
using Philiprehberger.MimeDetect;
byte[] data = File.ReadAllBytes("photo.png");
string? mime = MimeDetect.FromBytes(data);
// "image/png"
using Philiprehberger.MimeDetect;
byte[] header = new byte[] { 0x25, 0x50, 0x44, 0x46, 0x2D };
string? mime = MimeDetect.FromBytes(header);
// "application/pdf"
MimeResult? result = MimeDetect.DetectFromBytes(header);
// result.MimeType => "application/pdf"
// result.Extension => "pdf"
// result.Category => "document"
using Philiprehberger.MimeDetect;
string? mime = MimeDetect.FromExtension(".mp4");
// "video/mp4"
string? mime2 = MimeDetect.FromExtension("json");
// "application/json"
using Philiprehberger.MimeDetect;
string? mime = MimeDetect.FromFile("archive.zip");
// "application/zip"
MimeResult? result = MimeDetect.DetectFromFile("song.mp3");
// result.MimeType => "audio/mpeg"
// result.Extension => "mp3"
// result.Category => "audio"
using Philiprehberger.MimeDetect;
bool image = MimeDetect.IsImage("photo.png"); // true
bool video = MimeDetect.IsVideo("clip.mp4"); // true
bool audio = MimeDetect.IsAudio("song.mp3"); // true
bool archive = MimeDetect.IsArchive("backup.zip"); // true
MimeDetect.IsImage("readme.txt"); // false
MimeDetect| Method | Description |
|---|---|
FromBytes(ReadOnlySpan<byte> data) | Detect MIME type from raw bytes via magic byte matching |
FromExtension(string extension) | Look up MIME type by file extension |
FromFile(string path) | Detect MIME type from a file (magic bytes with extension fallback) |
DetectFromBytes(ReadOnlySpan<byte> data) | Detect MIME type from raw bytes, returns full MimeResult |
DetectFromFile(string path) | Detect MIME type from a file, returns full MimeResult |
IsImage(string path) | Check if a file is an image |
IsVideo(string path) | Check if a file is a video |
IsAudio(string path) | Check if a file is an audio file |
IsArchive(string path) | Check if a file is an archive |
MimeResult| Property | Type | Description |
|---|---|---|
MimeType | string | The MIME type string (e.g., "image/png") |
Extension | string | The canonical file extension without a leading dot |
Category | string | Broad category: image, video, audio, document, archive, application, text |
dotnet build src/Philiprehberger.MimeDetect.csproj --configuration Release
If you find this project useful: