Windows 注册表中的 Mime 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3442607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:00:16  来源:igfitidea点击:

Mime Types in the windows registry

c#windowsmime-types

提问by acadia

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

在我用 C# 构建的 Windows 服务中,我试图根据文件扩展名设置 mime 类型,如下所示

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.

在生产服务器上,由于显而易见的原因,我们没有安装 Acrobat 或 word。我该如何解决这个问题?有没有其他方法可以设置 mime 类型?如果不是,我如何在生产服务器上创建这些 mime 类型而不必安装这些软件。

Thanks in advance

提前致谢

回答by Rup

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"
  • 在 regedit 中转到 HKEY_LOCAL_MACHINE\Software\Classes(HKCR 的中心部分)
  • 右键单击类,新键 - 称之为 .pdf
  • 右键单击右侧,新字符串 - “内容类型”,“应用程序/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!

或者您可以找到一台安装了 Acrobat 的机器,然后使用 regedit 将 .pdf 密钥导出到您可以在您的服务器上导入的注册表脚本。您最终会得到很多脚本:您可以使用记事本将它们一起编辑,但请确保在保存时不要更改编码 - 它必须是 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.

您同样可以将映射列表添加到您的应用程序中,但是当 IIS 没有内容类型时,它不会提供静态内容,因此您可能也应该将它们添加到您的注册表中,以防您需要提供静态 PDF。