/// <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()); } 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 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); } }