Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

c# - How to get the icon associated with a specific folder?

I was required in one of my projects to get the icon for specific folders from their paths.

For example:
If I use C:UsersUsernameDesktop I want to get the icon associated with the Desktop folder
If I use the path of a folder that has a custom icon, I want to get that icon

AND NO, I DO NOT WANT THE GENERAL DEFAULT FOLDER ICON

I've been searching for nearly 3 days with no luck. Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use the native SHGetFileInfo function. You can import it using the .net InteropServices.

There's a knowledge base article describing how to do it.

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

EDIT:

Also

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

Example:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...