有一个简单的方法来确定一个文件是由它在C#中的扩展呢? [英] Is there an easy way to determine what a file is by its extension in C#?

查看:94
本文介绍了有一个简单的方法来确定一个文件是由它在C#中的扩展呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来确定一个文件是由它在C#中的扩展呢?例如,如果我通过名为.txt,那么它将返回文本文档文件扩展名,或者如果我通过它.PDF,它会返回Adobe Acrobat Reader软件。我看到这种行为内置到Windows资源管理器中,在类型一栏下。有没有办法模仿这在C#?

Is there an easy way to determine what a file is by its extension in C#? For example if I pass a file extension of ".txt" then it will return "Text Document" or if I pass it ".pdf" it will return "Adobe Acrobat Reader". I see this behavior built into Windows Explorer, under the "Type" column. Is there a way to mimic this in C#?

推荐答案

如果你想获得什么资源管理器实际显示,愿意使用COM跨运,你可以使用Shell.Application类代码的最低金额得到它。如果你去添加引用,浏览到X:\windows\system32\shell32.dll将导入SHELL32的类型库。然后,只需使用代码:

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);
}

这篇关于有一个简单的方法来确定一个文件是由它在C#中的扩展呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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