简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM