簡體   English   中英

C# 從文件路徑中獲取文件類型名稱

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

我已經實現了從文件路徑中獲取文件類型名稱所需的所有代碼,它運行成功,但它返回減去的類型名稱,例如,如果.pdb 或 .pdf文件的文件路徑,那么它將返回“obe Acrobat 文檔”而不是“Adobe 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"
}

這是返回文件類型名稱的屏幕截圖

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

它工作正常......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM