示例#1
0
        /// <summary>
        /// Gets the icon for the given path (file or folder).
        /// </summary>
        /// <param name="path">Path to file or folder.</param>
        /// <param name="smallIcon">True to get the small icon, false for large icon.</param>
        /// <param name="isDirectory">True if path is directory, otherwise false.</param>
        /// <param name="isOpen">True if path id directory and the open directory icon is wanted.</param>
        /// <returns>The icon for the given path.</returns>
        public static Icon GetIcon(string path, bool smallIcon, bool isDirectory, bool isOpen)
        {
            var flags     = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;
            var attribute = isDirectory ? Shell32.FILE_ATTRIBUTE_DIRECTORY : Shell32.SHGFI_ICONLOCATION;

            flags += (smallIcon) ? Shell32.SHGFI_SMALLICON : Shell32.SHGFI_LARGEICON;
            if (isDirectory && isOpen)
            {
                flags += Shell32.SHGFI_OPENICON;
            }

            var shfi = new Shell32.ShFileInfo {
                szDisplayName = string.Empty, szTypeName = string.Empty
            };
            var res = Shell32.SHGetFileInfo(Marshal.StringToBSTR(path), attribute, ref shfi, (uint)Marshal.SizeOf(shfi), flags);

            if (Equals(res, IntPtr.Zero))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            try
            {
                Icon.FromHandle(shfi.hIcon);
                return((Icon)Icon.FromHandle(shfi.hIcon).Clone());
            }
            catch
            {
                return(null);
            }
            finally
            {
                User32.DestroyIcon(shfi.hIcon);
            }
        }
示例#2
0
        /// <summary>
        /// Gets an image source for the given special folder.
        /// </summary>
        /// <param name="folder">Special folder.</param>
        /// <param name="smallIcon">True for small icon, otherwise false.</param>
        /// <returns>The image source of the icon for the requested special folder.</returns>
        public static ImageSource GetSpecialFolderIcon(Environment.SpecialFolder folder, bool smallIcon)
        {
            var shfi = new Shell32.ShFileInfo {
                szDisplayName = string.Empty, szTypeName = string.Empty
            };
            var ptrDir = IntPtr.Zero;

            Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, GetSpecialFolderCsidl(folder), ref ptrDir);
            var flags = Shell32.SHGFI_ICON | Shell32.SHGFI_PIDL;

            flags += (smallIcon) ? Shell32.SHGFI_SMALLICON : Shell32.SHGFI_LARGEICON;
            var res = Shell32.SHGetFileInfo(ptrDir, 0, ref shfi, (uint)Marshal.SizeOf(shfi), flags);

            if (Equals(res, IntPtr.Zero))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            Marshal.FreeCoTaskMem(ptrDir);
            var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

            User32.DestroyIcon(shfi.hIcon);
            var image = Imaging.CreateBitmapSourceFromHIcon(icon.Handle,
                                                            new Int32Rect(0, 0, icon.Width, icon.Height),
                                                            BitmapSizeOptions.FromEmptyOptions());

            icon.Dispose();

            return(image);
        }