C#获取由内容类型文件的扩展名 [英] C# Get file extension by content type

查看:604
本文介绍了C#获取由内容类型文件的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能通过内容类型文件的扩展名?



例我知道文件是文/ CSS这样的扩展会的CSS。

 私有静态字符串GetExtension(字符串的contentType)
{
串EXT =。

{这里判定代码}

返回分机;
}


解决方案

最佳的解决方案,我所知道的是查询注册表。你可以在这里找到示例代码。
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;
    }

这篇关于C#获取由内容类型文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆