Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner and I wonder how could I get the system's icon of a windows drive or folder with pure .NET Code - I am currently thinking of a one-liner or something like that.
I have tried already this:

C#
this.Icon=System.Drawing.Icon.ExtractAssociatedIcon(@"C:\");
this.Icon=System.Drawing.Icon.ExtractAssociatedIcon(@"C:\Windows");


Both fails with a FileNotFound Error. Can I convert the folder to file and then extract the icon and the convert it back? Or needs this function other parameters?
I search for a compact way without many external functions to extract a folder icon. Any ideas?

If needed those external API's does anyone have a class on the ready which could be used to read icons from folders, files and drives and put into a listview. I mean a class which is error-proof and suitable to read hundreds of icons at once and displays them in the listView? Have nothing found yet. Microsoft's MSDN code I have seen and studied but it didn't worked (no icons displayed) and OutOfMemory Exception occures on a icon count larger than 507.

Could not solve this yet so far got only icons from Files itself.
File icons have also very low quality and are not right windows icons if
I use this code below:
C#
this.Icon=System.Drawing.Icon.ExtractAssociatedIcon(@"C:\myfile.txt");

Found not another fully working code or tool yet for Folders and Drives.
Posted
Updated 21-Mar-22 4:18am
v4

AFAIK, you can't do it in pure .NET - you need to use a DLLImport: http://support2.microsoft.com/?kbid=319350[^]
 
Share this answer
 
Comments
OriginalGriff 2-Mar-15 5:06am    
From just that? No - what does the debugger say is happening?
public static class IconHelper
{
    [DllImport("gdi32.dll", SetLastError = true)]
    private static extern bool DeleteObject(IntPtr hObjetc);

    [DllImport("shell32")]
    private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint flags);

    private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
    private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;

    private const uint SHGFI_ICON = 0x000000100;
    private const uint SHGFI_SMALLICON = 0x000000001;
    private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;

    [StructLayout(LayoutKind.Sequential)]
    private 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 static Icon GetIconOfPath(string path, bool isSmallIcon, bool isDirectoryOrDrive)
    {
        uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES;
        if (isSmallIcon)
            flags |= SHGFI_SMALLICON;

        uint attributes = FILE_ATTRIBUTE_NORMAL;
        if (isDirectoryOrDrive)
            attributes |= FILE_ATTRIBUTE_DIRECTORY;

        int success = SHGetFileInfo(path, attributes, out SHFILEINFO shfi, (uint)Marshal.SizeOf(typeof(SHFILEINFO)), flags);

        if (success == 0)
            return null;

        return Icon.FromHandle(shfi.hIcon);
    }
}


path is just the path of the file/folder/drive you're getting,
isSmallIcon is self explanatory
isDirectoryOrDrive is if the path is not a file but a folder or drive
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900