dotnet-MimeType 1.0.0

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

dotnet-mimetype

A .NET library for detecting MIME types and file extensions based on file signatures (magic bytes) and predefined mappings. This project provides a robust way to identify file types from byte arrays, streams, or file paths, supporting both signature-based detection and extension-to-MIME type mappings.

Overview

The dotnet-mimetype library is designed to help developers identify file types and MIME types accurately in .NET applications. It supports detection through file signatures (magic bytes) for precise identification and includes a comprehensive mapping of MIME types to file extensions. The library is built with extensibility in mind, allowing developers to add support for additional file formats.

Features

  • File Signature Detection: Detects file types by analyzing magic bytes from files, streams, or byte arrays.
  • MIME Type Detection: Maps file extensions to their corresponding MIME types.
  • Extensible Architecture: Easily extendable to support additional file formats through custom signature checkers.
  • Thread-Safe Lazy Loading: Uses lazy-initialized collections for efficient and thread-safe access to file type and MIME type data.
  • Exception Handling: Provides clear error messages for invalid or non-readable inputs.

Installation

Clone the repository and include the project in your .NET solution, or build and reference the compiled DLL.

git clone https://github.com/ibrahimekinci/dotnet-mimetype.git

Usage

Detecting File Types by Signature

using MimeType.Services;
using MimeType.Core.Models;

var detector = new FileSignatureDetector();
byte[] fileBytes = File.ReadAllBytes("sample.png");
var fileTypes = detector.Detect(fileBytes);

foreach (var fileType in fileTypes)
{
    Console.WriteLine($"MIME: {fileType.Mime}, Extensions: {string.Join(", ", fileType.Extensions)}");
}

Detecting MIME Types by Extension

using MimeType.Services;

var mimeDetector = new MimeTypeDetector();
var mimeTypes = mimeDetector.Detect(".png");

foreach (var mime in mimeTypes)
{
    Console.WriteLine($"MIME Type: {mime}");
}

Detecting File Extensions by MIME Type

using MimeType.Services;

var extensionDetector = new FileExtensionDetector();
var extensions = extensionDetector.Detect("image/png");

foreach (var ext in extensions)
{
    Console.WriteLine($"Extension: {ext}");
}

Supported File Signatures

The library currently supports signature-based detection for the following image formats:

  • BMP (.bmp): Starts with "BM".
  • GIF (.gif): Starts with "GIF87a" or "GIF89a".
  • PNG (.png): Starts with 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A.
  • TIFF (.tiff, .tif): Starts with 0x49 0x49 0x2A 0x00 (II*) or 0x4D 0x4D 0x00 0x2A (MM*).
  • WebP (.webp): Starts with "RIFF" followed by "WEBP" at offset 8.
  • JPEG (.jpg, .jpeg, .jpe, .jfif): Starts with 0xFF 0xD8 0xFF.
  • JPEG XL (.jxl): Starts with 0xFF 0x0A (raw codestream) or a 12-byte container signature.
  • DWG (.dwg): Starts with "AC" followed by version strings like "1.40", "1002", etc.

Additional file formats (e.g., audio, video, and archive formats) are supported for MIME type-to-extension mappings but lack signature detection at this time. Support for these formats will be added in future updates.

Contributing

We welcome contributions to expand the supported file formats and improve the library! To contribute:

  1. Fork the repository: https://github.com/ibrahimekinci/dotnet-mimetype
  2. Add new file signature checkers or enhance existing ones in the MimeType.Infrastructure.FileSignatureCheckers namespace.
  3. Update the BuiltInFileTypes class to include new file signatures and mappings.
  4. Submit a pull request with a clear description of your changes.

Please ensure your code follows the existing structure and includes appropriate tests.

References

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Developed by Ibrahim Ekinci.

Product Compatible and additional computed target framework versions.
.NET 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 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.
  • 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 137 9/1/2025