private static string GetMimeFromFile(string filename)
{
if (File.Exists(filename) == false)
{
throw new FileNotFoundException(String.Format("File '{0}' not found", filename));
}
int numberOfBytesRead;
byte[] buffer = new byte[256];
using (var fs = new FileStream(filename, FileMode.Open))
{
var length = (int) Math.Min(256, fs.Length);
numberOfBytesRead = fs.Read(buffer, 0, length);
}
string result;
try
{
IntPtr mimetype;
FindMimeFromData(IntPtr.Zero, null, buffer, numberOfBytesRead, null, 0, out mimetype, 0);
result = Marshal.PtrToStringUni(mimetype);
Marshal.FreeCoTaskMem(mimetype);
}
catch (Exception e)
{
throw new ApplicationException(String.Format("Unable to determine type of file '{0}'.", filename), e);
}
return result;
}