加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在.NET中获取文件类型

发布时间:2020-12-15 06:25:36 所属栏目:百科 来源:网络整理
导读:如何使用c#获取文件的类型? 例如,如果文件名称id“abc.png”和文件类型将在窗口浏览器中将“PNG图像”与第三列“类型”相同. 解决方法 您将需要使用 Windows API SHGetFileInfo function 在输出结构中,szTypeName包含您要查找的名称. [StructLayout(LayoutK
如何使用c#获取文件的类型?
例如,如果文件名称id“abc.png”和文件类型将在窗口浏览器中将“PNG图像”与第三列“类型”相同.

解决方法

您将需要使用 Windows API SHGetFileInfo function

在输出结构中,szTypeName包含您要查找的名称.

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct SHFILEINFO
{
     public IntPtr hIcon;
     public int iIcon;
     public uint dwAttributes;

     [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 260)]
     public string szDisplayName;

     [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 80)]
     public string szTypeName;
};

请注意,这只是存储在Windows注册表中的当前“友好名称”,它只是一个标签(但对于您的情况可能足够好).

szTypeName和szDisplayName之间的区别描述在MSDN:

szTypeName: Null-terminated string that
describes the type of file.

szDisplayName: Null-terminated string
that contains the name of the file as
it appears in the Windows shell,or
the path and name of the file that
contains the icon representing the
file.

为了更准确地确定文件类型,您需要阅读每个文件的第一个字节块,并将其与已发布的文件规范进行比较.有关文件格式的信息,请参阅Wotsit网站.

链接页面还包含完整的示例C#代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读