Esempio n. 1
0
        /// <summary>
        ///     The current Windows default Folder Icon in the given Size (Large/Small) as System.Drawing.Icon.
        /// </summary>
        /// <param name="size">The Size of the Icon (Small or Large).</param>
        /// <param name="folderType">The folderTypeIcon (closed or Open).</param>
        /// <returns>The Folder Icon as System.Drawing.Icon.</returns>
        public static Icon GetDefaultDirectoryIcon(IconSize size, FolderType folderType)
        {
            var flags = ShgfiIcon | ShgfiUsefileattributes;

            if (FolderType.Open == folderType)
            {
                flags += ShgfiOpenicon;
            }
            if (IconSize.Small == size)
            {
                flags += ShgfiSmallicon;
            }
            else
            {
                flags += ShgfiLargeicon;
            }
            var shfi = new Structs.Shfileinfo();
            var res  = DllImports.SHGetFileInfo(@"C:\Windows", FileAttributeDirectory, out shfi, (uint)Marshal.SizeOf(shfi), flags);

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

            DllImports.DestroyIcon(shfi.hIcon);
            return(icon);
        }