Skip to content

Instantly share code, notes, and snippets.

@city41
Created May 5, 2011 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save city41/958009 to your computer and use it in GitHub Desktop.
Save city41/958009 to your computer and use it in GitHub Desktop.
Get content type from file extension
public string GetContentType(string filename)
{
string mimeType = "application/unknown";
string extension = Path.GetExtension(filename);
if (string.IsNullOrWhiteSpace(extension))
{
return mimeType;
}
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(extension.ToLower());
if (regKey != null)
{
object contentType = regKey.GetValue("Content Type");
if (contentType != null)
{
mimeType = contentType.ToString();
}
}
return mimeType;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment