/// <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>Icon</returns> public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay) { var shfi = new Shell32.SHFILEINFO(); var flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY; /* Check the size specified for return. */ if (IconSize.Small == size) { flags += Shell32.SHGFI_SMALLICON; } else { flags += Shell32.SHGFI_LARGEICON; } //Shell32.SHGetFileInfo(name, // Shell32.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 //var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); //User32.DestroyIcon(shfi.hIcon); // Cleanup //return icon; return GetIcon(flags, name, Shell32.FILE_ATTRIBUTE_NORMAL); }
private static Icon GetIcon(uint flags, string path, uint fileAttributes) { var shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfo(path, fileAttributes, ref shfi, (uint)Marshal.SizeOf(shfi), flags); 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 var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon(shfi.hIcon); // Cleanup return(icon); }
private static Icon GetIcon(uint flags, string path, uint fileAttributes) { var shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfo(path, fileAttributes, ref shfi, (uint) Marshal.SizeOf(shfi), flags); 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 var icon = (Icon) Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon(shfi.hIcon); // Cleanup return icon; }