且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#获取由内容类型文件的扩展名

更新时间:2022-10-15 18:00:29

***的解决方案,我所知道的是查询注册表。你可以在这里找到示例代码。
http://cyotek.com/blog/mime-types-and-file-extensions

 公共静态字符串GetDefaultExtension(字符串mime类型)
{
字符串结果;
的RegistryKey关键;
对象的值;

键= Registry.ClassesRoot.OpenSubKey(@MIME\Database\Content Type\+ mime类型,FALSE);
=键的值!= NULL? key.GetValue(扩展,NULL):空;
结果=值!= NULL? value.ToString()的String.Empty;

返回结果;
}


How can I get file extension by content type ?

Example I know that file is "text/css" so extension will be ".css".

private static string GetExtension(string contentType)
{
    string ext = ".";

    { DETERMINATION CODE IN HERE }

    return ext;
}

The "Best" solution that I know of is to query the registry. You can find example code here. http://cyotek.com/blog/mime-types-and-file-extensions

 public static string GetDefaultExtension(string mimeType)
    {
      string result;
      RegistryKey key;
      object value;

      key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
      value = key != null ? key.GetValue("Extension", null) : null;
      result = value != null ? value.ToString() : string.Empty;

      return result;
    }