Is there an easy way to determine what a file is by its extension in C#?

≡放荡痞女 提交于 2019-11-30 16:23:35

If you want to get what explorer actually shows and are willing to use COM inter-op you can use the Shell.Application class to get it with the minimum amount of code. If you go to add a reference, browse to X:\windows\system32\shell32.dll that will import shell32's type library. Then just use the code:

string GetFileType(string path)
{
   Shell32.ShellClass shell = new Shell32.ShellClass();
   Shell32.Folder folder = shell.NameSpace(Path.GetDirectoryName(path));

   Shell32.FolderItem item = folder.ParseName(Path.GetFileName(path));
   return folder.GetDetailsOf(item, 2);
}

Use the Registry class to query the HKCR hive.

Have a look at this class

C# FileAssociation Class

Check the registry; you can get that data from there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!