title: C# Incorrect Mimetype returned application/octet-stream
body: <p>I have pilfered a chuck of code from pinkode.net I believe it was - that will find the mimetype of any file. It works great and its much needed. When I push the code to our SSIS package that will call the method, it always returns the same mimetype, application/octet-stream. This is a very narrow use of this method in that I am only evaluating .xls? files. It runs fine on my workstation VS project, but the same code on the server runs fine but returns the same type.</p>
  
  <p>What/where is this kind of behavior defined on a windows 10 server? File associations getting in the way? I am completely at a loss as to why it is not working and havent really tried. I could find and retain the mimetype for the files I know of, but the goal of this ETL process was to be able to parse up any excel to a raw state. Getting mimetype from file is critical to the stated goal and this has been working fine until it lives on a db server.
  ...</p>
  
  <pre><code>public static string getMimeFromFile(string file)
      {
          IntPtr mimeout;
          if (!System.IO.File.Exists(file))
              throw new FileNotFoundException(file + " not found");
  
          int MaxContent = (int)new FileInfo(file).Length;
          if (MaxContent &gt; 4096) MaxContent = 4096;
          FileStream fs = File.OpenRead(file);
  
  
          byte[] buf = new byte[MaxContent];
          fs.Read(buf, 0, MaxContent);
          fs.Close();
          int result = FindMimeFromData(IntPtr.Zero, file, buf, MaxContent, null, 0, out mimeout, 0);
  
          if (result != 0)
              throw Marshal.GetExceptionForHR(result);
          string mime = Marshal.PtrToStringUni(mimeout);
          Marshal.FreeCoTaskMem(mimeout);
          return mime;
      }
  </code></pre>
  
  <p>...</p>