【问题标题】:C# Get File Type Name from file pathC# 从文件路径获取文件类型名称
【发布时间】:2019-09-01 18:26:59
【问题描述】:

我已经实现了从文件路径获取文件类型名称所需的所有代码,并且它工作成功,但它返回减去的类型名称,例如如果 .pdb 或 .pdf 文件的文件路径,那么它将返回 "obe Acrobat Document" 而不是 "Adobe Acrobat Document"

我使用了 shell32.dll。我不明白发生了什么,请帮我摆脱它。

源代码:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    internal struct SHFILEINFO
    {            
        public IntPtr hIcon;            
        public IntPtr iIcon;            
        public uint dwAttributes;            
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;            
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };

internal class Win32
{            
    public const uint FILE_ATTRIBUTE_NORMAL = 0x80;            
    public const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;            
    public const uint SHGFI_TYPENAME = 0x000000400;            
    public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;            
    internal const uint SHGFI_SYSICONINDEX = 0x000004000;            
    internal const int ILD_TRANSPARENT = 0x1;            
    internal const uint SHGFI_ICON = 0x100;            
    internal const uint SHGFI_LARGEICON = 0x0; 
    internal const uint SHGFI_SMALLICON = 0x1; 

    [DllImport("shell32.dll", CharSet=CharSet.Unicode)]
    internal static extern IntPtr SHGetFileInfo
        (
            string pszPath, 
            uint dwFileAttributes, 
            ref SHFILEINFO psfi, 
            uint cbSizeFileInfo, 
            uint uFlags
        );

    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    internal static extern int ExtractIconEx
        (
            string stExeFileName, 
            int nIconIndex, 
            ref IntPtr phiconLarge, 
            ref IntPtr phiconSmall, 
            int nIcons
        );

    [DllImport("comctl32.dll", SetLastError = true)]
    internal static extern IntPtr ImageList_GetIcon
        (
            IntPtr himl, 
            int i, 
            int flags
        );

    [DllImport("user32.dll")]
    internal static extern bool DestroyIcon(IntPtr hIcon);
}

internal static string GetFileType(string filename)
{
    SHFILEINFO shinfo = new SHFILEINFO();
    Win32.SHGetFileInfo
        (
                filename,
                Win32.FILE_ATTRIBUTE_NORMAL,
                ref shinfo, (uint)Marshal.SizeOf(shinfo),
                Win32.SHGFI_TYPENAME |
                Win32.SHGFI_USEFILEATTRIBUTES
            );

    return shinfo.szTypeName; //It return "obe Acrobat Document" Instead of "Adobe Acrobat Document"
}

【问题讨论】:

  • 嗯,你的代码对我来说非常适合......
  • 但在我的项目中它返回如屏幕截图所示。我找不到原因

标签: c#


【解决方案1】:

C++ 结构中的 iIcon 字段的类型为 int。所以,我只需设置 iIcon 的类型为 int,而不是 IntPtr。 IntPtr 根据系统平台工作。我只是将 int 类型设置为 iIcon 之类的,

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
internal 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;
};

而且它工作正常......

【讨论】:

  • 请在此答案中总结解决方案,因为引用其他来源的链接可能会随着时间的推移而消失或更改,如果发生这种情况,此特定答案对任何人都没有帮助。
猜你喜欢
  • 1970-01-01
  • 2014-07-24
  • 2011-04-13
  • 2011-07-10
  • 2011-01-25
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多