---
title: "How do you get the MIME type of a file in C#?"  
description: "How do you get the MIME type of a file in C#?"  
author: "ICSM Computer"  
published: 2025-05-16  
updated: 2025-05-23  
canonical: https://www.mindstick.com/forum/161629/how-do-you-get-the-mime-type-of-a-file-in-c-sharp  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you get the MIME type of a file in C#?

How do you get the [MIME type](https://www.mindstick.com/interview/1730/what-is-mime-type-of-json) of a [file in C#](https://www.mindstick.com/forum/159903/how-to-create-a-log-file-in-c-sharp)? with example.

## Replies

### Reply by Anubhav Sharma

In C#, there are several ways to determine the MIME type of a file. Here are the most common approaches:

## 1. Use `Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider` (Recommended in ASP.NET Core)

```cs
using Microsoft.AspNetCore.StaticFiles;

var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType("example.pdf", out string contentType))
{
    contentType = "application/octet-stream"; // default fallback
}
Console.WriteLine(contentType); // Outputs: application/pdf
```

- **Pros:** Reliable, easy, extensible
- **Requires:** `Microsoft.AspNetCore.StaticFiles` NuGet package

## 2. Use Windows Registry via `System.Web.MimeMapping` (Only on Windows / ASP.NET)

```cs
string mimeType = System.Web.MimeMapping.GetMimeMapping("file.docx");
Console.WriteLine(mimeType); // Outputs: application/vnd.openxmlformats-officedocument.wordprocessingml.document
```

- **Pros:** Simple to use
- **Limitations:** Windows-only, available only in **.NET Framework** (not .NET Core)

## 3. Use `Registry` (Windows-only)

```cs
using Microsoft.Win32;

string GetMimeType(string fileName)
{
    string extension = Path.GetExtension(fileName)?.ToLowerInvariant();
    if (extension == null) return "application/octet-stream";

    RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);
    return key?.GetValue("Content Type") as string ?? "application/octet-stream";
}
```

**Platform-specific** — Windows only.

## 4. Use File Content (Magic Numbers) with `MimeDetective` (Advanced)

If you want to inspect the actual **file content** (not just the extension), use third-party libraries like:

- [MimeDetective](https://github.com/Muraad/Mime-Detective)
- [HeyRed.Mime](https://github.com/HeyRed/Mime)

**Example using MimeDetective**:

```cs
using MimeDetective;

var inspector = new ContentInspectorBuilder()
    .AddDefaults()
    .Build();

var fileInfo = new FileInfo("example.png");
var result = inspector.Inspect(fileInfo);
Console.WriteLine(result?.MimeType); // Outputs: image/png
```

## Best for file content validation (e.g., to detect forged extensions)

## Summary

| Method | Works in .NET Core | Platform | Based on | Accuracy |
| --- | --- | --- | --- | --- |
| `FileExtensionContentTypeProvider` | Yes | Cross-platform | File extension | Medium |
| `System.Web.MimeMapping` | No | Windows only (.NET Framework) | File extension | Medium |
| Registry access | NO | Windows only | File extension | Medium |
| MimeDetective or HeyRed.Mime | Yes | Cross-platform | File content | High |

## Read More -

- [How do you identify and prevent directory traversal attacks in file uploads?](https://www.mindstick.com/forum/161637/how-do-you-identify-and-prevent-directory-traversal-attacks-in-file-uploads)
- [S. Jaishankar: Pakistan has a list of terrorists that needs to be handed over to India.](https://yourviews.mindstick.com/view/88128/s-jaishankar-pakistan-has-a-list-of-terrorists-that-needs-to-be-handed-over-to-india)
- [Best Dabba Trading Platform: A Smarter Way to Trade in Today's Economy](https://answers.mindstick.com/qa/114687/best-dabba-trading-platform-a-smarter-way-to-trade-in-today-s-economy)


---

Original Source: https://www.mindstick.com/forum/161629/how-do-you-get-the-mime-type-of-a-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
