/// <summary> /// Used to access system folder icons. /// </summary> /// <param name="size">Specify large or small icons.</param> /// <param name="folderType">Specify open or closed FolderType.</param> /// <returns>System.Drawing.Icon</returns> public static Icon GetFolderIcon(IconSize size, FolderType folderType) { // Need to add size check, although errors generated at present! uint flags = NativeMethodsShell32.SHGFI_ICON | NativeMethodsShell32.SHGFI_USEFILEATTRIBUTES; if (FolderType.Open == folderType) { flags += NativeMethodsShell32.SHGFI_OPENICON; } if (IconSize.Small == size) { flags += NativeMethodsShell32.SHGFI_SMALLICON; } else { flags += NativeMethodsShell32.SHGFI_LARGEICON; } // Get the folder icon NativeMethodsShell32.SHFILEINFO shfi = new NativeMethodsShell32.SHFILEINFO(); NativeMethodsShell32.SHGetFileInfo(System.AppDomain.CurrentDomain.BaseDirectory, NativeMethodsShell32.FILE_ATTRIBUTE_DIRECTORY, ref shfi, (uint)Marshal.SizeOf(shfi), flags); System.Drawing.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 Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); NativeMethodsUser32.DestroyIcon(shfi.hIcon); // Cleanup return(icon); }
/// <summary> /// Returns an icon for a given file - indicated by the name parameter. /// </summary> /// <param name="name">Pathname for file.</param> /// <param name="size">Large or small</param> /// <param name="linkOverlay">Whether to include the link icon</param> /// <returns>System.Drawing.Icon</returns> public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay) { NativeMethodsShell32.SHFILEINFO shfi = new NativeMethodsShell32.SHFILEINFO(); uint flags = NativeMethodsShell32.SHGFI_ICON | NativeMethodsShell32.SHGFI_USEFILEATTRIBUTES; if (linkOverlay) { flags |= NativeMethodsShell32.SHGFI_LINKOVERLAY; } /* Check the size specified for return. */ if (IconSize.Small == size) { flags |= NativeMethodsShell32.SHGFI_SMALLICON; } else { flags |= NativeMethodsShell32.SHGFI_LARGEICON; } NativeMethodsShell32.SHGetFileInfo(name, NativeMethodsShell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags); // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); NativeMethodsUser32.DestroyIcon(shfi.hIcon); // Cleanup return(icon); }