关于Content Type类型的获取

在以前写代码的时候,有的时候会遇到根据文件的扩展名来获得Content Type,一般的做法是做一个数组,把常用的扩展名和Content Type做一个一一对应的关系。

但还有一种方法,是利用Windows的注册表来进行查找。

代码如下:

public static string GetContentType(string fileName){
    string mime = "application/octetstream";
    Microsoft.Win32.RegistryKey rk =
        Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(Path.GetExtension(fileName));
    if (rk != null && rk.GetValue("Content Type") != null){
        mime = rk.GetValue("Content Type").ToString();
    }
    return mime;
}
          

也算是一种小技巧了。:

原文地址:https://www.cnblogs.com/ajiebp1977/p/2171109.html