Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mime Types in the windows registry

In my windows service built with C# I am trying to set the mime types based on the file extensions as shown below

static string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = Path.GetExtension(fileName).ToLower();

            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);

            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();

            return mimeType;
        } 

On the production server we don't have Acrobat or word installed for obvious reasons. How do I get around with this? Is there any other way of setting the mime types? If not how do I create those mime types on the production server without having to install those softwares.

Thanks in advance

like image 290
acadia Avatar asked Aug 09 '10 17:08

acadia


People also ask

How do I know my MIME type?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

Where are MIME types stored?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop. org.

What are MIME file types?

A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF's RFC 6838.

How do I change the MIME type of a file in Windows?

Windows makes it very convenient to associate programs and file type Right click the file and open up properties. From here, under the general tab, you can choose which app opens this specific file type. Hit change, and navigate to your shim. bat.


1 Answers

You can simply create the registry entries for those types for your code to read

  • in regedit go to HKEY_LOCAL_MACHINE\Software\Classes (the central part of HKCR)
  • right click Classes, New Key - call it .pdf
  • right click on the right hand side, New String - "Content Type", "application/pdf"

or you can find a machine that does have Acrobat installed and then use regedit to export the .pdf key to a registry script you can import on your server. You'll end up with lots of scripts: you can use notepad to edit them together but make sure you don't change the encoding when you save - it has to be Unicode!

You could equally add a list of mappings into your application but IIS won't serve static content when it doesn't have a content type so you should probably add them to your registry too in case you ever need to serve static PDFs.

like image 60
Rup Avatar answered Oct 16 '22 23:10

Rup