Jan
04
2007
Getting mimetype Windows
Posted by admin under
.NET
This code needs permission to read from registry - so it might not be suited in a web scenario. However it's useful so here it is:
private string MimeType(string Filename)
{
string mime = "application/octetstream";
string ext = System.IO.Path.GetExtension(Filename).ToLower();
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (rk != null && rk.GetValue("Content Type") != null)
mime = rk.GetValue("Content Type").ToString();
return mime;
}
In short - you just feed it with path top a file - and get back the mime type.
Ex
MimeType("f:\\theimage.jpg") --->>> "image/jpeg"
Sample usage (Windows Forms application) can be found here in the context of Inserting blob into MySQL