MimeMagic 4.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package MimeMagic --version 4.0.1
                    
NuGet\Install-Package MimeMagic -Version 4.0.1
                    
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="MimeMagic" Version="4.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MimeMagic" Version="4.0.1" />
                    
Directory.Packages.props
<PackageReference Include="MimeMagic" />
                    
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 MimeMagic --version 4.0.1
                    
#r "nuget: MimeMagic, 4.0.1"
                    
#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 MimeMagic@4.0.1
                    
#: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=MimeMagic&version=4.0.1
                    
Install as a Cake Addin
#tool nuget:?package=MimeMagic&version=4.0.1
                    
Install as a Cake Tool

Mime

.NET wrapper for libmagic

NuGet license

Install

dotnet add package MimeMagic

MimeMagic is published to nuget.org from this repository, a fork of hey-red/Mime.

Only the NuGet package name differs from hey-red/Mime. No code changes should be needed to switch from the Mime package to MimeMagic, as the assembly name remains Mime and the namespace is still HeyRed.Mime. Avoid referencing both packages in the same project; their assemblies and native assets will conflict.

Why fork?

  • Keeps the bundled libmagic native binaries, .NET runtimes and referenced packages up to date
  • Detects large files correctly (hey-red/Mime#62)
  • Builds the libmagic native binaries in GitHub workflows, so they can be reproduced and checked
  • Uses current C# and .NET conventions and project structure

Requirements

Supported .NET versions are the current LTS releases (net8.0 and net10.0), plus the latest STS release when the library uses an API the STS adds. LTS targets are dropped once they reach end of support. An STS target is replaced by the next LTS once that ships, even though the STS is still supported.

Supported runtimes:

  • linux-arm64
  • linux-musl-arm64
  • linux-musl-x64
  • linux-x64
  • osx-arm64
  • osx-x64
  • win-arm64 (not currently tested due to lack of GitHub runner)
  • win-x64
  • win-x86

Basic usage

using HeyRed.Mime;

// (Optionally) You can set path to magic database file manually.
MimeGuesser.MagicFilePath = "/path/to/magic.mgc";

// Guess mime type of file(overloaded method takes byte array or stream as arg.)
MimeGuesser.GuessMimeType("path/to/file"); //=> image/jpeg

// Get extension of file(overloaded method takes byte array or stream as arg.)
MimeGuesser.GuessExtension("path/to/file"); //=> jpeg

// Get mime type and extension of file(overloaded method takes byte array or stream as arg.)
MimeGuesser.GuessFileType("path/to/file"); //=> FileType

Advanced

Want more than just the mime type? Use the Magic class:

string calc = @"C:\Windows\System32\calc.exe";
using var magic = new Magic(MagicOpenFlags.MAGIC_NONE);
magic.Read(calc); //=> PE32+ executable (GUI) x86-64, for MS Windows

// Check encoding:
string textFile = @"F:\Temp\file.txt";
using var magic = new Magic(MagicOpenFlags.MAGIC_MIME_ENCODING);
magic.Read(textFile); //=> Output: utf-8

Also, we can combine flags with "|" operator. See all flags for more info.

Remarks

  • The Magic class is not thread safe, but if you use different instances on different threads it seems to work fine.
  • The MimeGuesser is thread safe, since it generates a new instance of Magic class on each use.

Building

git clone https://github.com/si618/Mime.git
cd Mime
dotnet build
dotnet test --no-build
dotnet run --project samples/MimeExample/MimeExample.csproj
dotnet pack -c Release -o nupkg

Releasing

MimeMagic is published to nuget.org by the Pack workflow, which runs only when a v* tag is pushed. Pushes and pull requests to master only build and test (see CI).

Publishing a release

  1. Make sure master is green in CI and contains everything intended for the release.

  2. Pick the version following SemVer. The package version comes from the tag, not from <Version> in src/Mime/Mime.csproj, which is only the default for local builds. Keep it in step with the tag anyway.

  3. Tag the master commit and push the tag:

    git switch master
    git pull
    git tag -a v4.0.0 -m "MimeMagic 4.0.0"
    git push origin v4.0.0
    

    A pre-release tag such as v4.1.0-beta.1 publishes a pre-release package.

  4. Watch the Pack run under the repository's Actions tab. It runs the tests on net8.0 and net10.0, packs with the version taken from the tag (v4.0.0 becomes 4.0.0), and pushes the .nupkg and .snupkg to nuget.org. The packages are also attached to the run as an artifact.

  5. The new version appears on the MimeMagic package page after nuget.org finishes validating and indexing it, which usually takes a few minutes.

Versions on nuget.org are immutable, so a published version can't be replaced. To fix a bad release, unlist it on nuget.org and tag a new version. Re-pushing an existing version is skipped (--skip-duplicate).

Publishing setup

Publishing uses nuget.org Trusted Publishing. The workflow exchanges its GitHub OIDC token for a short-lived API key, so no long-lived key is stored. This is already configured, and only needs redoing if the repository, the workflow file name or the nuget.org owner changes:

  • nuget.org Trusted Publishing policy: owner si-fi, repository owner si618, repository Mime, workflow file pack.yml, no environment, scoped to the package MimeMagic.
  • GitHub repository secret NUGET_USER: the nuget.org username (si-fi). This must be a repository secret, not an environment secret, because the Pack job doesn't declare a GitHub environment.

Possible problems

Exception Solution
DllNotFoundException Make sure your bin folder contains the runtimes directory. If publishing a platform-dependent app, bin should contain libmagic-1 (.dll, .so, or .dylib) and magic.mgc.
BadImageFormatException Try targeting x64 or arm64 instead of AnyCPU.
MagicException: Could not find any valid magic files! Make sure magic.mgc is in one of the /runtimes/ subdirs or alongside libmagic-1.[dll\|lib\|dylib]. Or set a custom path as described in basic usage.

License

MIT

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 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.

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
4.0.3 1,326 10/1/2026
4.0.2 68 9/30/2026
4.0.1 66 9/30/2026
4.0.0 64 9/30/2026