/// <summary>
        /// Get the icon for the specified file
        /// </summary>
        /// <param name="filePath">path to file</param>
        /// <param name="iconType">icon type (small or large)</param>
        /// <returns>IconHandle (or null if none found). IconHandle represents
        /// a Win32 HICON. It must be Disposed when the caller is finished with
        /// it to free the underlying resources used by the Icon. </returns>
        private static IconHandle GetIconForFile(string filePath, uint iconType)
        {
            // allocate SHFILEINFO for holding results
            SHFILEINFO fileInfo = new SHFILEINFO();

            // get icon info
            IntPtr result = Shell32.SHGetFileInfo(filePath, 0, ref fileInfo,
                (uint)Marshal.SizeOf(fileInfo), SHGFI.ICON | iconType);
            if (result == IntPtr.Zero)
            {
                Debug.Fail("Error getting icon for file: " + Marshal.GetLastWin32Error());
                return null;

            }

            // return IconHandle
            return new IconHandle(fileInfo.hIcon);
        }
        /// <summary>
        /// Get the icon for the specified file extension
        /// </summary>
        /// <param name="extension">file extension (including leading .)</param>
        /// <param name="flags">icon type (small or large)</param>
        /// <returns>IconHandle (or null if none found). IconHandle represents
        /// a Win32 HICON. It must be Disposed when the caller is finished with
        /// it to free the underlying resources used by the Icon. </returns>
        private static IconHandle GetIconForExtension(string extension, uint flags)
        {
            // allocate SHFILEINFO for holding results
            SHFILEINFO fileInfo = new SHFILEINFO();

            // get icon info for file extension
            IntPtr result = Shell32.SHGetFileInfo(extension, FILE_ATTRIBUTE.NORMAL, ref fileInfo,
                (uint)Marshal.SizeOf(fileInfo), SHGFI.ICON | flags | SHGFI.USEFILEATTRIBUTES);
            if (result == IntPtr.Zero)
            {
                Debug.Fail("Error getting icon for file: " + Marshal.GetLastWin32Error());
                return null;
            }

            // return IconHandle
            return new IconHandle(fileInfo.hIcon);
        }
Example #3
0
 public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);