public static extern IntPtr SHGetFileInfo(string pszPath, FileAttributes dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
private static Image GetSpecialFolderIcon(string clsid, ref Size size) { Image image; IShellFolder desktopFolder = ShellItem.GetDesktopFolder(); try { uint num; IntPtr ptr; SFGAO pdwAttributes = 0; desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, clsid, out num, out ptr, ref pdwAttributes); SHFILEINFO psfi = new SHFILEINFO(); Shell32.SHGetFileInfo(ptr, FileAttributes.Directory, ref psfi, (SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_PIDL | SHGFI.SHGFI_USEFILEATTRIBUTES | SHGFI.SHGFI_ICON) | ((size.Height < 0x20) ? SHGFI.SHGFI_SMALLICON : SHGFI.SHGFI_LARGEICON)); image = IconToImage(psfi.hIcon); } catch (ArgumentException) { image = null; } finally { Marshal.ReleaseComObject(desktopFolder); } return image; }
public static IntPtr SHGetFileInfo(string pszPath, FileAttributes dwFileAttributes, ref SHFILEINFO psfi, SHGFI Flags) { return SHGetFileInfo(pszPath, dwFileAttributes, ref psfi, (uint) Marshal.SizeOf(((SHFILEINFO) psfi).GetType()), (uint) Flags); }
private Image GetIcon(string fileName, FileAttributes attributes, bool useAttributes, ref Size size) { Image image; IDictionary<Size, Image> dictionary; Dictionary<int, IDictionary<Size, Image>> dictionary2; SHFILEINFO psfi = new SHFILEINFO(); Shell32.SHGetFileInfo(fileName, attributes, ref psfi, (useAttributes ? (SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES) : SHGFI.SHGFI_LARGEICON) | (SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_SYSICONINDEX)); lock ((dictionary2 = this.IconCache)) { if (this.IconCache.TryGetValue(psfi.iIcon, out dictionary)) { if (dictionary == null) { return null; } if (dictionary.TryGetValue(size, out image)) { return image; } } } psfi = new SHFILEINFO(); Shell32.SHGetFileInfo(fileName, attributes, ref psfi, ((useAttributes ? (SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES) : SHGFI.SHGFI_LARGEICON) | (SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_ICON)) | ((size.Height < 0x20) ? SHGFI.SHGFI_SMALLICON : SHGFI.SHGFI_LARGEICON)); image = IconToImage(psfi.hIcon); lock ((dictionary2 = this.IconCache)) { if (dictionary != null) { dictionary.Add(size, image); } else { dictionary = IconCollection.Create(); dictionary.Add(size, image); this.IconCache.Add(psfi.iIcon, dictionary); } } return image; }