c# - 如何使用 Shell32.SHGetFileInfo 在 Windows 7 上获取文件夹图标

标签 c# .net shell windows-7

我有以下适用于 Windows XP 和 Vista 的代码 - 32 位和 64 位:

public static Icon GetFolderIcon(IconSize size, FolderType folderType)
{
    // Need to add size check, although errors generated at present!
    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

    if (FolderType.Open == folderType)
    {
        flags += Shell32.SHGFI_OPENICON;
    }

    if (IconSize.Small == size)
    {
       flags += Shell32.SHGFI_SMALLICON;
    } 
    else 
    {
       flags += Shell32.SHGFI_LARGEICON;
    }

    // Get the folder icon
    var shfi = new Shell32.SHFILEINFO();
    Shell32.SHGetFileInfo(  null, 
                            Shell32.FILE_ATTRIBUTE_DIRECTORY, 
                            ref shfi, 
                            (uint) Marshal.SizeOf(shfi), 
                            flags );

    Icon.FromHandle(shfi.hIcon);    // Load the icon from an HICON handle

    // Now clone the icon, so that it can be successfully stored in an ImageList
    var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

    User32Dll.DestroyIcon( shfi.hIcon );        // Cleanup
    return icon;
}

常量定义如下:

public const uint SHGFI_ICON = 0x000000100;
public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
public const uint SHGFI_OPENICON = 0x000000002;
public const uint SHGFI_SMALLICON = 0x000000001;
public const uint SHGFI_LARGEICON = 0x000000000;
public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;

获取文件夹图标时,这会在 Windows 7 中产生以下结果:

alt text

在 Vista 中 - 使用相同的方法会产生以下文件夹图标:

alt text

我也想要 Windows 7 的“正确”Windows 文件夹图标 - 而不是用于指示安装 Windows 的驱动器的图标。

我不知道 win32 API,我的非托管编程在 Windows 平台上几乎没有。

最佳答案

您不应将 null 指定为 SHGeFileInfo 的第一个参数。请改用文件夹的路径(请注意,某些文件夹具有不同的(非标准)图标)。例如,您可以使用临时文件夹或应用程序的根文件夹。

最佳做法是为每个文件夹获取正确的图标(换句话说:将 GetFolderIcon 的签名更改为 public static Icon GetFolderIcon(string folderPath, IconSize size, FolderType folderType) 并为您显示的每个文件夹调用它)。

似乎有一个开源库,它已经有一个用于获取文件夹图标的托管包装器。 在 PInvoke.net 上找到(SHGetFileInfo 的条目):

However, this does not work if you want an icon of a drive or folder.

In that case, you can use the ExtendedFileInfo class provided by the ManagedWindowsApi project (http://mwinapi.sourceforge.net).

如果您想坚持使用手工制作的解决方案,这对我很有效(Win7 x64 RTM、.NET 3.5 SP1):

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace IconExtractor
{
    [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;
    };

    public enum FolderType
    {
        Closed,
        Open
    }

    public enum IconSize
    {
        Large,
        Small
    }

    public partial class Form1 : Form
    {
        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool DestroyIcon(IntPtr hIcon);

        public const uint SHGFI_ICON = 0x000000100; 
        public const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; 
        public const uint SHGFI_OPENICON = 0x000000002; 
        public const uint SHGFI_SMALLICON = 0x000000001; 
        public const uint SHGFI_LARGEICON = 0x000000000; 
        public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;

        public static Icon GetFolderIcon(IconSize size, FolderType folderType)
        {    
            // Need to add size check, although errors generated at present!    
            uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;    

            if (FolderType.Open == folderType)    
            {        
                flags += SHGFI_OPENICON;    
            }    
            if (IconSize.Small == size)    
            {       flags += SHGFI_SMALLICON;    
            }     
            else     
            {       
                flags += SHGFI_LARGEICON;    
            }    
            // Get the folder icon    
            var shfi = new SHFILEINFO();    

            var res = SHGetFileInfo(@"C:\Windows",                             
                FILE_ATTRIBUTE_DIRECTORY,                             
                out shfi,                             
                (uint) Marshal.SizeOf(shfi),                             
                flags );

            if (res == IntPtr.Zero)
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());

            // Load the icon from an HICON handle  
            Icon.FromHandle(shfi.hIcon);    

            // Now clone the icon, so that it can be successfully stored in an ImageList
            var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();    

            DestroyIcon( shfi.hIcon );        // Cleanup    

            return icon;}

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {

                Icon icon = GetFolderIcon(IconSize.Large, FolderType.Open);
                pictureBox1.Image = icon.ToBitmap();
                // Note: The image actually should be disposed somewhere
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

关于c# - 如何使用 Shell32.SHGetFileInfo 在 Windows 7 上获取文件夹图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1599235/

相关文章:

c# - 在 Visual Studio 中搜索特定类型

.net - 在 .NET DLL 中存储连接字符串的最佳方式是什么?

c# - 三层文化特定资源文件

linux - 使用shell脚本删除文件夹内容

linux - Bourne shell 构建字符串并将其用作参数

c# - 使用来自 VB6 代码的 C# 程序集。一些问题

c# - 在 LINQ 查询内部或外部放置条件有区别吗?

c# - C# 列表中的不相交联合

.net - 如何在 Release模式下为.net托管项目生成PDB?

shell - echo >> 样式追加,但到文件的开头