Skip to content
Simple MIME type guesser for .NET and .NET Core.
C#
Branch: master
Clone or download
Latest commit 68fc361 Apr 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Mime Update key location Oct 23, 2018
test/MimeTests Update deps Apr 15, 2019
.gitattributes :neckbeard: Added .gitattributes & .gitignore files Sep 26, 2016
.gitignore Initial commit Sep 26, 2016
LICENSE Update copyright May 15, 2018
Mime.sln Update solution Aug 17, 2017
README.md Updated info about SCD in readme Aug 17, 2017
x64.png added picture for README Jun 14, 2017

README.md

Mime

Simple MIME type guesser for .NET and .NET Core.

NuGet license

Install

via NuGet:

PM> Install-Package Mime

Requirements

Supports only x64 OS(Linux, MacOS and Windows). If you run into issues, make sure you set your target platform to x64: x64.png

Basic usage

using HeyRed.Mime;

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

// 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";
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";
var magic = new Magic(MagicOpenFlags.MAGIC_MIME_ENCODING);
magic.Read(textFile); //=> Output: utf-8

Also, we can combine flags with "|" operator. For all flag options, see this

Remarks

  • The Magic class is not thread safe, but if you use different instances on different threads it seems to work fine.
  • The MimeGuesser seems to be thread safe, since it generates a new instance of Magic class on each use.
  • If you publish application as Self-contained(SCD) with target RID linux/OSX(?), then make sure to set magic path manually.

License

MIT

You can’t perform that action at this time.