c# - 如何获取与特定文件夹关联的图标?

标签 c# winforms icons pinvoke directory

我的一个项目要求我从路径中获取特定文件夹的图标。

例如:
如果我使用 C:\Users\Username\Desktop 我想获取与桌面文件夹关联的图标
如果我使用具有自定义图标的文件夹的路径,我想获取该图标

不,我不想要一般的默认文件夹图标

我已经搜索了将近 3 天,但没有找到。感谢您的帮助。

最佳答案

您可以使用 native SHGetFileInfo 函数。您可以使用 .net InteropServices 导入它。

有一篇知识库文章描述了如何做到这一点。

http://support.microsoft.com/kb/319350

编辑:

还有

How do I fetch the folder icon on Windows 7 using Shell32.SHGetFileInfo

例子:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace IconTest2
{
    public partial class MainWindow : Window
    {
        //Struct used by SHGetFileInfo function
        [StructLayout(LayoutKind.Sequential)]
        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;
        };

        //Constants flags for SHGetFileInfo 
        public const uint SHGFI_ICON = 0x100;
      public const uint SHGFI_LARGEICON = 0x0; // 'Large icon

        //Import SHGetFileInfo function
     [DllImport("shell32.dll")]
     public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

            public MainWindow()
            {
                InitializeComponent();
                SHFILEINFO shinfo = new SHFILEINFO();

                //Call function with the path to the folder you want the icon for
                SHGetFileInfo(
                    "C:\\Users\\Public\\Music",
                    0, ref shinfo,(uint)Marshal.SizeOf(shinfo),
                    SHGFI_ICON | SHGFI_LARGEICON);

                using (Icon i = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                {
                    //Convert icon to a Bitmap source
                    ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
                                            i.Handle,
                                            new Int32Rect(0, 0, i.Width, i.Height),
                                            BitmapSizeOptions.FromEmptyOptions());

                    //WPF Image control
                    m_image.Source = img;
                }
            }
        }
    }

SHGetFileInfo 引用 - http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx

关于c# - 如何获取与特定文件夹关联的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23666076/

相关文章:

c# - 使用 Windows 形式的命令行参数

c# - 如何创建包含 IPv4 地址的文本框?

java - 从 Java 中的图像的一部分创建图像?

html - 将图标添加到 Bootstrap 下拉菜单

android - 在抽屉导航中的项目旁边添加图标

c# - Azure 应用服务中的 IWebHostEnvironment ContentRootPath 与本地不同

c# - 过滤数据表中的列

c# - 方法的数据类型是尽可能具体还是更通用更好?

c# - 游戏网络弹丸实现/概念问题

DataGridView 中的 C# ComboBox 不可点击