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

如何按内容类型获取文件扩展名?

示例我知道该文件是“text / css”,因此扩展名为“.css”。

private static string GetExtension(string contentType) { string ext = "."; { DETERMINATION CODE IN HERE } return ext; } 

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