content type by extension

后端 未结 6 1181
悲哀的现实
悲哀的现实 2020-12-03 03:50

Is there any built in function that returns the content type based on the file extension?

6条回答
  •  隐瞒了意图╮
    2020-12-03 04:13

    Not that I know of. But you can use this code:

    using Microsoft.Win32;
    
    RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);
    string contentType = key.GetValue("Content Type").ToString();
    

    You'll need to add extra code for error handling.

    Note: The extension needs to be prefixed by a dot, like in .txt.

提交回复
热议问题