简体   繁体   English

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

[英]C# Get File Type Name from file path

I have implemented all required code to get file type name from file path and it is working successfully but it returns subtracted type name eg if file path of .pdb or .pdf file then it will return "obe Acrobat Document" instead of "Adobe Acrobat Document"我已经实现了从文件路径中获取文件类型名称所需的所有代码,它运行成功,但它返回减去的类型名称,例如,如果.pdb 或 .pdf文件的文件路径,那么它将返回“obe Acrobat 文档”而不是“Adobe Acrobat文档”

I have used shell32.dll.我用过shell32.dll。 I am not getting what happens please, help me to get rid out of it.我不明白发生了什么,请帮我摆脱它。

Source code:源代码:

[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"
}

这是返回文件类型名称的屏幕截图

The iIcon field in the C++ struct has type int.So, I just have to set the type of iIcon int only, not IntPtr. C++ 结构体中的 iIcon 字段的类型是 int。所以,我只需要设置 iIcon int 的类型,而不是 IntPtr。 IntPtr works as per system platform so. IntPtr 根据系统平台工作。 I just set int type to iIcon like,我只是将 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;
};

And It's working fine...它工作正常......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM