/// <summary> /// lookup and return an icon from windows shell /// </summary> /// <param name="name">path to the file</param> /// <param name="size">large or small</param> /// <param name="linkOverlay">true to include the overlay link iconlet</param> /// <returns>requested icon</returns> public static System.Drawing.Icon GetFileIcon( string filePath, EnumIconSize size, bool addLinkOverlay) { EnumFileInfoFlags flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES; // add link overlay if requested if (addLinkOverlay) { flags |= EnumFileInfoFlags.LINKOVERLAY; } // set size if (size == EnumIconSize.Small) { flags |= EnumFileInfoFlags.SMALLICON; } else { flags |= EnumFileInfoFlags.LARGEICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo( filePath, conFILE_ATTRIBUTE_NORMAL, ref shellFileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return(icon); }
/// <summary> /// lookup and return an icon from windows shell /// </summary> /// <param name="folderPath">path to folder<param> /// <param name="size">large or small</param> /// <param name="folderType">open or closed</param> /// <returns>requested icon</returns> public static System.Drawing.Icon GetFolderIcon( string folderPath, EnumIconSize size, EnumFolderType folderType) { EnumFileInfoFlags flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES; if (folderType == EnumFolderType.Open) { flags |= EnumFileInfoFlags.OPENICON; } if (EnumIconSize.Small == size) { flags |= EnumFileInfoFlags.SMALLICON; } else { flags |= EnumFileInfoFlags.LARGEICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo( folderPath, conFILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return(icon); }