public int AddSystemIcon(string filename) { string tempFile = GetTempFileOfType(filename); try { SHFILEINFO shFileInfoSmallIcon = new SHFILEINFO(); SHFILEINFO shFileInfoLargeIcon = new SHFILEINFO(); IntPtr hSmallIcon = WindowsAPI.SHGetFileInfo(tempFile, 256, ref shFileInfoSmallIcon, (uint) Marshal.SizeOf(shFileInfoSmallIcon), WindowsAPI.SHGFI_ICON | WindowsAPI.SHGFI_SMALLICON | WindowsAPI.SGHFI_USEFILEATTRIBUTES); IntPtr hLargeIcon = WindowsAPI.SHGetFileInfo(tempFile, 256, ref shFileInfoLargeIcon, (uint) Marshal.SizeOf(shFileInfoLargeIcon), WindowsAPI.SHGFI_ICON | WindowsAPI.SHGFI_LARGEICON | WindowsAPI.SGHFI_USEFILEATTRIBUTES); System.Drawing.Icon smallIcon = System.Drawing.Icon.FromHandle(shFileInfoSmallIcon.hIcon); System.Drawing.Icon largeIcon = System.Drawing.Icon.FromHandle(shFileInfoLargeIcon.hIcon); int retIndex = m_smallIcons.Images.Count; m_smallIcons.Images.Add(smallIcon); WindowsAPI.DestroyIcon(smallIcon.Handle); m_largeIcons.Images.Add(largeIcon); WindowsAPI.DestroyIcon(largeIcon.Handle); return retIndex; } finally { File.Delete(tempFile); } }
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);