Exemplo n.º 1
0
        /// <summary>
        /// Returns an icon for a given file - indicated by the name parameter.
        /// </summary>
        /// <param name="name">Pathname for file.</param>
        /// <param name="size">Large or small</param>
        /// <param name="linkOverlay">Whether to include the link icon</param>
        /// <returns>System.Drawing.Icon</returns>
        public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
        {
            var shfi  = new Shell32.Shfileinfo();
            var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;

            if (linkOverlay)
            {
                flags += Shell32.ShgfiLinkoverlay;
            }
            /* Check the size specified for return. */
            if (IconSize.Small == size)
            {
                flags += Shell32.ShgfiSmallicon;
            }
            else
            {
                flags += Shell32.ShgfiLargeicon;
            }
            Shell32.SHGetFileInfo(name,
                                  Shell32.FileAttributeNormal,
                                  ref shfi,
                                  (uint)Marshal.SizeOf(shfi),
                                  flags);
            // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
            var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

            User32.DestroyIcon(shfi.hIcon);     // Cleanup
            return(icon);
        }
Exemplo n.º 2
0
            public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
            {
                var shfi  = new Shell32.Shfileinfo();
                var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;

                if (linkOverlay)
                {
                    flags += Shell32.ShgfiLinkoverlay;
                }

                if (IconSize.Small == size)
                {
                    flags += Shell32.ShgfiSmallicon;
                }
                else
                {
                    flags += Shell32.ShgfiLargeicon;
                }

                Shell32.SHGetFileInfo(name,
                                      Shell32.FileAttributeNormal,
                                      ref shfi,
                                      (uint)Marshal.SizeOf(shfi),
                                      flags);

                var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

                User32.DestroyIcon(shfi.hIcon);

                return(icon);
            }
Exemplo n.º 3
0
        public static Icon GetFolderIcon(String path, IconSize iconSize, FolderType folderType)
        {
            var flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES | (UInt32)iconSize | (UInt32)folderType;

            var shfi = new Shell32.Shfileinfo {
            };

            var res = Shell32.SHGetFileInfo(
                path,
                Shell32.FILEATTRIBUTE_DIRECTORY,
                ref shfi,
                (UInt32)Marshal.SizeOf(shfi),
                flags);

            if (Object.Equals(res, IntPtr.Zero))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            try
            {
                return((Icon)Icon.FromHandle(shfi.hIcon).Clone());
            }
            catch
            {
                throw;
            }
            finally
            {
                User32.DestroyIcon(shfi.hIcon);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns an icon for a given file - indicated by the name parameter.
        /// </summary>
        /// <param name="path">Pathname for file.</param>
        /// <param name="iconSize">Large or small</param>
        /// <param name="linkOverlay">Whether to include the link icon</param>
        /// <returns>System.Drawing.Icon</returns>
        public static Icon GetFileIcon(String path, IconSize iconSize, Boolean linkOverlay = false)
        {
            var flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES | (UInt32)iconSize;

            if (linkOverlay)
            {
                flags += Shell32.SHGFI_LINKOVERLAY;
            }

            var shfi = new Shell32.Shfileinfo {
            };

            var res = Shell32.SHGetFileInfo(
                path,
                Shell32.FILEATTRIBUTE_NORMAL,
                ref shfi,
                (UInt32)Marshal.SizeOf(shfi),
                flags);

            if (Object.Equals(res, IntPtr.Zero))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            try
            {
                return((Icon)Icon.FromHandle(shfi.hIcon).Clone());
            }
            catch
            {
                throw;
            }
            finally
            {
                User32.DestroyIcon(shfi.hIcon);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Returns an icon for a given file - indicated by the name parameter.
 /// </summary>
 /// <param name="name">Pathname for file.</param>
 /// <param name="size">Large or small</param>
 /// <param name="linkOverlay">Whether to include the link icon</param>
 /// <returns>System.Drawing.Icon</returns>
 public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
 {
     var shfi = new Shell32.Shfileinfo();
     var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes;
     if (linkOverlay) flags += Shell32.ShgfiLinkoverlay;
     /* Check the size specified for return. */
     if (IconSize.Small == size)
         flags += Shell32.ShgfiSmallicon;
     else
         flags += Shell32.ShgfiLargeicon;
     Shell32.SHGetFileInfo(name,
         Shell32.FileAttributeNormal,
         ref shfi,
         (uint)Marshal.SizeOf(shfi),
         flags);
     // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
     var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
     User32.DestroyIcon(shfi.hIcon);     // Cleanup
     return icon;
 }