private static extern int SHGetFileInfo( string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
public static extern IntPtr SHGetFileInfo(IntPtr pszPath, int dwFileAttributes, out SHFILEINFO psfi, int cbFileInfo, SHGFI uFlags);
/// <summary> /// Gets the information for the specified /// file name and flags. /// </summary> public void GetInfo() { ShellIcon = null; TypeName = ""; DisplayName = ""; SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); int ret = SHGetFileInfo(FileName, 0, ref shfi, shfiSize, (uint)(Flags)); if (ret != 0) { if (shfi.hIcon != IntPtr.Zero) { ShellIcon = System.Drawing.Icon.FromHandle(shfi.hIcon); // Now owned by the GDI+ object //DestroyIcon(shfi.hIcon); } TypeName = shfi.szTypeName; DisplayName = shfi.szDisplayName; } else { int err = GetLastError(); Console.WriteLine($"Error {err}"); string txtS = new string('\0', 256); int len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, IntPtr.Zero, err, 0, txtS, 256, 0); Console.WriteLine($"Len {len} text {txtS}"); } }
public int GetSystemImageListIndex(IntPtr pidl, ShellIconType type, ShellIconFlags flags) { var info = new SHFILEINFO(); IntPtr result = Shell32.SHGetFileInfo(pidl, 0, out info, Marshal.SizeOf(info), SHGFI.Icon | SHGFI.SysIconIndex | SHGFI.OverlayIndex | SHGFI.PIDL | (SHGFI)type | (SHGFI)flags); if (result == IntPtr.Zero) { throw new Exception("Error retrieving shell folder icon"); } User32.DestroyIcon(info.hIcon); return info.iIcon; }
/* public void SetSize(int size) { ImageList_Destroy(this.Handle); this._ImageList = Marshal.GetObjectForIUnknown(ImageList_Create(size, size, 0x00020000 | 0x00000020, 30, 30)) as IImageList2; this._ImageList.SetImageCount(3000); } private Lazy<Int32Size> _SizePixels; public ImageListSize Size { get { return this._Size; } } public int Width { get { return this._SizePixels.Value.Width; } } public int Height { get { return this._SizePixels.Value.Height; } } */ /* private IntPtr Handle { get { return Marshal.GetIUnknownForObject(this._ImageList); } } */ /* public static ImageListSize MaxSize { get { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { if (Environment.OSVersion.Version.Major >= 6) { return ImageListSize.Jumbo; } else { return ImageListSize.ExtraLarge; } } return ImageListSize.Jumbo; } } */ #endregion #region Method /* [Obsolete("Consider Inlining")] private int GetIconIndex(IntPtr path) { var options = SHGetFileInfoOptions.SysIconIndex | SHGetFileInfoOptions.Pidl; var shfi = new SHFILEINFO(); var shfiSize = Marshal.SizeOf(shfi.GetType()); IntPtr retVal = Win32Api.SHGetFileInfo(path, FileAttributes.None, ref shfi, shfiSize, options); if (shfi.hIcon != IntPtr.Zero) { Win32Api.DestroyIcon(shfi.hIcon); } if (retVal.Equals(IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } else { return shfi.iIcon; } } */ /// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="overlayIndex">Index of the overlay icon that use for Draw or GetIndexOfOverlay method.</param> /// <returns></returns> public int GetIconIndexWithOverlay(IntPtr path, out int overlayIndex) { var options = SHGetFileInfoOptions.SysIconIndex | SHGetFileInfoOptions.OverlayIndex | SHGetFileInfoOptions.Icon | SHGetFileInfoOptions.AddOverlays | SHGetFileInfoOptions.Pidl; var shfi = new SHFILEINFO(); var shfiSize = Marshal.SizeOf(shfi.GetType()); IntPtr retVal = Win32Api.SHGetFileInfo(path, FileAttributes.None, ref shfi, shfiSize, options); if (shfi.hIcon != IntPtr.Zero) { Win32Api.DestroyIcon(shfi.hIcon); } if (retVal.Equals(IntPtr.Zero)) { //throw new Win32Exception(Marshal.GetLastWin32Error()); overlayIndex = 0; return 0; } else { /* brakes stack on optimized build int idx = shfi.iIcon & 0xFFFFFF; int iOverlay = shfi.iIcon >> 24; overlayIndex = iOverlay; return idx; */ overlayIndex = shfi.iIcon >> 24; return shfi.iIcon & 0xFFFFFF; } }
public static extern IntPtr SHGetFileInfo(IntPtr pszPath, FileAttributes attr, ref SHFILEINFO psfi, int cbSizeFileInfo, SHGetFileInfoOptions uFlags);
private void UpdateIconCacheForFolder(String wszPath) { var sfi = new SHFILEINFO(); var res = Shell32.SHGetFileInfo(Marshal.StringToHGlobalAuto(wszPath), 0, out sfi, (Int32)Marshal.SizeOf(sfi), SHGFI.IconLocation); Int32 iIconIndex = Shell32.Shell_GetCachedImageIndex(sfi.szDisplayName.Replace(@"\\", @"\"), sfi.iIcon, 0); Shell32.SHUpdateImage(sfi.szDisplayName.Replace(@"\\", @"\"), sfi.iIcon, 0x0002, iIconIndex); Shell32.SHChangeNotify(Shell32.HChangeNotifyEventID.SHCNE_UPDATEIMAGE, Shell32.HChangeNotifyFlags.SHCNF_DWORD | Shell32.HChangeNotifyFlags.SHCNF_FLUSHNOWAIT, IntPtr.Zero, (IntPtr)sfi.iIcon); }