public int IconIndex(string fileName, bool forceLoadFromDisk, ShellIconStateConstants iconState) { SysImageList.SHGetFileInfoConstants sHGetFileInfoConstants = SysImageList.SHGetFileInfoConstants.SHGFI_SYSICONINDEX; if (this.size == SysImageListSize.smallIcons) { sHGetFileInfoConstants |= SysImageList.SHGetFileInfoConstants.SHGFI_SMALLICON; } int dwFileAttributes; if (!forceLoadFromDisk) { sHGetFileInfoConstants |= SysImageList.SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; dwFileAttributes = 128; } else { dwFileAttributes = 0; } SysImageList.SHFILEINFO sHFILEINFO = default(SysImageList.SHFILEINFO); uint cbFileInfo = (uint)Marshal.SizeOf(sHFILEINFO.GetType()); IntPtr intPtr = SysImageList.SHGetFileInfo(fileName, dwFileAttributes, ref sHFILEINFO, cbFileInfo, (uint)(sHGetFileInfoConstants | (SysImageList.SHGetFileInfoConstants)iconState)); int result; if (intPtr.Equals(IntPtr.Zero)) { Debug.Assert(!intPtr.Equals(IntPtr.Zero), "Failed to get icon index"); result = 0; } else { result = sHFILEINFO.iIcon; } return(result); }
private void create() { this.hIml = IntPtr.Zero; if (this.isXpOrAbove()) { Guid guid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); int num = SysImageList.SHGetImageList((int)this.size, ref guid, ref this.iImageList); SysImageList.SHGetImageListHandle((int)this.size, ref guid, ref this.hIml); } else { SysImageList.SHGetFileInfoConstants sHGetFileInfoConstants = SysImageList.SHGetFileInfoConstants.SHGFI_SYSICONINDEX | SysImageList.SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; if (this.size == SysImageListSize.smallIcons) { sHGetFileInfoConstants |= SysImageList.SHGetFileInfoConstants.SHGFI_SMALLICON; } SysImageList.SHFILEINFO sHFILEINFO = default(SysImageList.SHFILEINFO); uint cbFileInfo = (uint)Marshal.SizeOf(sHFILEINFO.GetType()); this.hIml = SysImageList.SHGetFileInfo(".txt", 128, ref sHFILEINFO, cbFileInfo, (uint)sHGetFileInfoConstants); Debug.Assert(this.hIml != IntPtr.Zero, "Failed to create Image List"); } }
private static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref SysImageList.SHFILEINFO psfi, uint cbFileInfo, uint uFlags);