C#从文件路径获取文件类型名称

时间:2019-04-11 07:36:45

标签: c#

我已经实现了所有必需的代码,以从文件路径获取文件类型名称,并且该代码成功运行,但返回了减去的类型名称,例如如果 .pdb或.pdf 文件的文件路径,则它将返回“ obe Acrobat文档” ,而不是“ Adob​​e Acrobat文档”

>

我使用过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"
}

This is screenshot of return file type name

1 个答案:

答案 0 :(得分:1)

C ++结构中的iIcon字段的类型为int.So,我只需要设置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;
};

而且工作正常...