Ejemplo n.º 1
0
        public static Icon IconFromExtensionUsingRegistry(string extension, SystemIconSize size)
        {
            if (extension == null)
            {
                throw new ArgumentNullException("extension");
            }
            if (extension.Length == 0 || extension == ".")
            {
                throw new ArgumentException("Empty extension", "extension");
            }

            if (extension[0] != '.')
            {
                extension = '.' + extension;
            }

            var iconLocation = GetExtensionIconStringFromRegistry(extension);

            if (iconLocation == null)
            {
                return(null);
            }

            return(ExtractFromRegistryString(iconLocation, size));
        }
Ejemplo n.º 2
0
        //this will look throw the registry
        //to find if the Extension have an icon.
        public static Icon IconFromExtension(string extension,
                                             SystemIconSize size)
        {
            // Add the '.' to the extension if needed
            if (extension[0] != '.')
            {
                extension = '.' + extension;
            }

            //opens the registry for the wanted key.
            RegistryKey Root         = Registry.ClassesRoot;
            RegistryKey ExtensionKey = Root.OpenSubKey(extension);

            ExtensionKey.GetValueNames();
            RegistryKey ApplicationKey =
                Root.OpenSubKey(ExtensionKey.GetValue("").ToString());

            //gets the name of the file that have the icon.
            string IconLocation =
                ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();

            string[] IconPath = IconLocation.Split(',');

            if (IconPath[1] == null)
            {
                IconPath[1] = "0";
            }
            IntPtr[] Large = new IntPtr[1], Small = new IntPtr[1];

            //extracts the icon from the file.
            ExtractIconEx(IconPath[0],
                          Convert.ToInt16(IconPath[1]), Large, Small, 1);
            return(size == SystemIconSize.Large ?
                   Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]));
        }
Ejemplo n.º 3
0
        public static Icon FromExtension(string fileOrExtension, SystemIconSize size)
        {
            if (fileOrExtension == null)
            {
                throw new ArgumentNullException(nameof(fileOrExtension), PublicResources.ArgumentNull);
            }
            if (!Enum <SystemIconSize> .IsDefined(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), PublicResources.EnumOutOfRangeWithValues(size));
            }
            if (!OSUtils.IsWindows)
            {
                return(SystemIcons.WinLogo);
            }

            if (!Path.HasExtension(fileOrExtension))
            {
                fileOrExtension = Path.GetFileName(fileOrExtension) == fileOrExtension ? '.' + fileOrExtension : ".";
            }

            IntPtr handle = Shell32.GetFileIconHandle(fileOrExtension, size);

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException(PublicResources.ArgumentInvalidString, nameof(fileOrExtension));
            }

            return(Icon.FromHandle(handle).ToManagedIcon());
        }
Ejemplo n.º 4
0
        internal static IntPtr GetFileIconHandle(string extension, SystemIconSize size)
        {
            var tempFileInfo = new SHFILEINFO();

            NativeMethods.SHGetFileInfo(extension, 0, ref tempFileInfo, (uint)Marshal.SizeOf(tempFileInfo), SGHFI.SHGFI_ICON | SGHFI.SHGFI_USEFILEATTRIBUTES | (SGHFI)size);
            return(tempFileInfo.hIcon);
        }
Ejemplo n.º 5
0
        public static Icon IconFromExtension(string extension)
        {
            SystemIconSize size = SystemIconSize.Small;

            if (extension == null)
            {
                throw new ArgumentNullException("extension");
            }
            if (extension.Length == 0 || extension == ".")
            {
                throw new ArgumentException("Empty extension", "extension");
            }

            if (extension[0] != '.')
            {
                extension = '.' + extension;
            }

            var fileInfo = new SHFILEINFO();

            SHGetFileInfo(extension, 0, out fileInfo, Marshal.SizeOf(fileInfo),
                          FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags)size);

            return(Icon.FromHandle(fileInfo.hIcon));
        }
Ejemplo n.º 6
0
        public static Icon[] FromFile(string fileName, SystemIconSize size)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName), PublicResources.ArgumentNull);
            }
            if (!Enum <SystemIconSize> .IsDefined(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), PublicResources.EnumOutOfRangeWithValues(size));
            }
            if (!OSUtils.IsWindows)
            {
                Reflector.EmptyArray <Icon>();
            }

            IntPtr[][] handles = Shell32.ExtractIconHandles(fileName, size);
            Icon[]     result  = new Icon[handles.Length];

            for (int i = 0; i < handles.Length; i++)
            {
                result[i] = Icon.FromHandle(handles[i][0]).ToManagedIcon();
            }

            return(result);
        }
Ejemplo n.º 7
0
        internal static IntPtr GetStockIconHandle(StockIcon id, SystemIconSize size)
        {
            var iconInfo = new SHSTOCKICONINFO {
                cbSize = (uint)Marshal.SizeOf(typeof(SHSTOCKICONINFO))
            };

            return(NativeMethods.SHGetStockIconInfo(id, SHGSI.ICON | (SHGSI)size, ref iconInfo) == 0 ? iconInfo.hIcon : IntPtr.Zero);
        }
        public static Icon ExtractFromRegistryString(string _regString_, SystemIconSize _size_)
        {
            string fileName;
            int    index;

            ExtractInformationsFromRegistryString(_regString_, out fileName, out index);
            return(ExtractOne(fileName, index, _size_));
        }
Ejemplo n.º 9
0
 public static Icon ExtractOne(string fileName, int index, SystemIconSize size)
 {
     try {
         List <Icon> iconList = ExtractEx(fileName, size, index, 1);
         return(iconList[0]);
     } catch (UnableToExtractIconsException) {
         throw new IconNotFoundException(fileName, index);
     }
 }
 public static Icon ExtractOne(string _fileName_, int _index_, SystemIconSize _size_)
 {
     try
     {
         List <Icon> iconList = ExtractEx(_fileName_, _size_, _index_, 1);
         return(iconList[0]);
     }
     catch (UnableToExtractIconsException)
     {
         throw new IconNotFoundException(_fileName_, _index_);
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets a native system icon as a managed <see cref="BitmapSource"/>.
        /// </summary>
        /// <param name="icon">The system icon.</param>
        /// <param name="iconSize">The system icon size.</param>
        /// <returns>Returns the system icon as <see cref="BitmapSource"/>.</returns>
        public static BitmapSource GetSystemIcon(SystemIcon icon, SystemIconSize iconSize)
        {
            NativeMethods.SHSTOCKICONINFO sii = default(NativeMethods.SHSTOCKICONINFO);
            sii.cbSize = (uint)Marshal.SizeOf(typeof(NativeMethods.SHSTOCKICONINFO));

            Marshal.ThrowExceptionForHR(NativeMethods.SHGetStockIconInfo((NativeMethods.SHSTOCKICONID)icon, (NativeMethods.SHGSI)iconSize | NativeMethods.SHGSI.SHGSI_ICON, ref sii));

            var iconSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(sii.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            NativeMethods.DestroyIcon(sii.hIcon);
            return(iconSource);
        }
Ejemplo n.º 12
0
        public static Icon IconFromExtension(string extension, SystemIconSize size)
        {
            RegistryKey currentUser = Registry.CurrentUser;
            var className = "Unknown";
            var classKey = currentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\"+extension+@"\OpenWithProgids");
            if (classKey != null)
                className = classKey.GetValueNames().First();
            RegistryKey Root = Registry.ClassesRoot;
            if (className.EndsWith("Folder"))
                className = "Folder";
            if (className.Equals("icofile"))
                className = "Unknown";

            RegistryKey ApplicationKey = Root.OpenSubKey(className);

            RegistryKey CurrentVer = null;
            try
            {
                CurrentVer = Root.OpenSubKey(ApplicationKey.OpenSubKey("CurVer").GetValue("").ToString());
            }
            catch (Exception) { }

            if (CurrentVer != null)
                ApplicationKey = CurrentVer;

            if (ApplicationKey == null)
                ApplicationKey = Root.OpenSubKey("Unknown");

            string IconLocation =
                ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();
            string[] IconPath = IconLocation.Split(',');

            IntPtr[] Large = null;
            IntPtr[] Small = null;
            int iIconPathNumber = 0;

            if (IconPath.Length > 1)
                iIconPathNumber = 1;
            else
                iIconPathNumber = 0;

            if (IconPath[iIconPathNumber] == null) IconPath[iIconPathNumber] = "0";
            Large = new IntPtr[1];
            Small = new IntPtr[1];
            if (iIconPathNumber > 0)
                ExtractIconEx(IconPath[0], Convert.ToInt16(IconPath[iIconPathNumber]), Large, Small, 1);
            else
                ExtractIconEx(IconPath[0], Convert.ToInt16(0), Large, Small, 1);

            return size == SystemIconSize.Large ? Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]);
        }
Ejemplo n.º 13
0
        public static Icon GetFolderIcon(SystemIconSize size, SystemFolderType systemFolderType)
        {
            var fileInfo = new SHFILEINFO();

            SHGetFileInfo(System.Environment.CurrentDirectory, (int)FILE_ATTRIBUTE_DIRECTORY, out fileInfo,
                          Marshal.SizeOf(fileInfo),
                          FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags)size |
                          (FileInfoFlags)systemFolderType);

            var fromHandle = Icon.FromHandle(fileInfo.hIcon).Clone();

            DestroyIcon(fileInfo.hIcon);
            return((Icon)fromHandle);
        }
Ejemplo n.º 14
0
        public static Icon ExtractFromRegistryString(string regString, SystemIconSize size)
        {
            string fileName;
            int    index;

            ExtractInformationsFromRegistryString(regString, out fileName, out index);
            try
            {
                return(ExtractOne(fileName, index, size));
            }
            catch (IconNotFoundException)
            {
                return(null);
            }
        }
Ejemplo n.º 15
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        ///
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public static Icon IconFromExtensionShell(string extension, SystemIconSize size)
        {
            //add '.' if nessesry
            if (extension[0] != '.')
            {
                extension = '.' + extension;
            }

            //temp struct for getting file shell info
            SHFILEINFO fileInfo = new SHFILEINFO();

            SHGetFileInfo(extension, 0, out fileInfo, Marshal.SizeOf(fileInfo),
                          FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags)size);

            return(Icon.FromHandle(fileInfo.hIcon));
        }
Ejemplo n.º 16
0
        public static Icon IconFromExtensionShell(string extension, SystemIconSize size)
        {
            //add '.' if nessesry
            if (extension[0] != '.') extension = '.' + extension;

            //temp struct for getting fileName shell info
            SHFILEINFO fileInfo = new SHFILEINFO();

            SHGetFileInfo(
                extension,
                0,
                out fileInfo,
                Marshal.SizeOf(fileInfo),
                FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags)size);

            return Icon.FromHandle(fileInfo.hIcon);
        }
        //this will look throw the registry
        //to find if the Extension have an icon.
        public static Icon IconFromExtension(string _extension_,
                                             SystemIconSize _size_)
        {
            // Add the '.' to the extension if needed
            if (_extension_[0] != '.')
            {
                _extension_ = '.' + _extension_;
            }

            //opens the registry for the wanted key.
            RegistryKey root         = Registry.ClassesRoot;
            RegistryKey extensionKey = root.OpenSubKey(_extension_);

            if (extensionKey != null)
            {
                extensionKey.GetValueNames();
                RegistryKey applicationKey =
                    root.OpenSubKey(extensionKey.GetValue("").ToString());

                //gets the name of the file that have the icon.
                if (applicationKey != null)
                {
                    string iconLocation =
                        applicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();
                    List <string> iconPath = iconLocation.Split(',').ToList();
                    if (iconPath.Count == 1)
                    {
                        iconPath.Add("0");
                    }
                    if (iconPath[1] == null)
                    {
                        iconPath[1] = "0";
                    }
                    IntPtr[] large = new IntPtr[1], small = new IntPtr[1];

                    //extracts the icon from the file.
                    ExtractIconEx(iconPath[0],
                                  Convert.ToInt16(iconPath[1]), large, small, 1);
                    return(_size_ == SystemIconSize.Large ?
                           Icon.FromHandle(large[0]) : Icon.FromHandle(small[0]));
                }
            }
            return(null);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Return a bitmap of the file icon, based on the file extension
        /// </summary>
        /// <param name="path">File path or filename containing the extension</param>
        /// <param name="largeIcons">Small or Large</param>
        /// <returns></returns>
        public static Bitmap FileIcon(string path, bool largeIcons = false, bool shellMethod = false)
        {
            Icon           icon = null;
            SystemIconSize size = largeIcons ? SystemIconSize.Large : SystemIconSize.Small;

            if (shellMethod)
            {
                icon = IconFromExtensionShell(path.Extension(), size);
            }
            else
            {
                icon = IconFromExtension(path.Extension(), size);
            }
            if (icon != null)
            {
                return(icon.ToBitmap());
            }
            return(null);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Used to access system folder icons.
        /// </summary>
        /// <param name="size">Specify large or small icons.</param>
        /// <param name="folderType">Specify open or closed FolderType.</param>
        /// <returns>System.Drawing.Icon</returns>
        public static System.Drawing.Icon GetFolderIcon(SystemIconSize size, FolderType folderType)
        {
            // Need to add size check, although errors generated at present!
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

            if (FolderType.Open == folderType)
            {
                flags += Shell32.SHGFI_OPENICON;
            }

            if (SystemIconSize.Small == size)
            {
                flags += Shell32.SHGFI_SMALLICON;
            }
            else
            {
                flags += Shell32.SHGFI_LARGEICON;
            }

            // Get the folder icon
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
            Shell32.SHGetFileInfo("dummy",                // Windows 7 needs a string passed to this regardless. Previous versions of windows could handle null
                                  Shell32.FILE_ATTRIBUTE_DIRECTORY,
                                  ref shfi,
                                  (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                  flags);

            if (shfi.hIcon != IntPtr.Zero)
            {
                System.Drawing.Icon.FromHandle(shfi.hIcon);                     // Load the icon from an HICON handle

                // Now clone the icon, so that it can be successfully stored in an ImageList
                System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();

                User32.DestroyIcon(shfi.hIcon);                         // Cleanup
                return(icon);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 20
0
        private static List <Icon> ExtractEx(string fileName, SystemIconSize size, int firstIconIndex, int iconCount)
        {
            List <Icon> iconList = new List <Icon>();

            switch (size)
            {
            case SystemIconSize.Large:
                ExtractEx(fileName, iconList, null, firstIconIndex, iconCount);
                break;

            case SystemIconSize.Small:
                ExtractEx(fileName, null, iconList, firstIconIndex, iconCount);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(size));
            }

            return(iconList);
        }
        public static Icon IconFromExtensionShell(string _extension_, SystemIconSize _size_)
        {
            //add '.' if nessesry
            if (_extension_[0] != '.')
            {
                _extension_ = '.' + _extension_;
            }

            //temp struct for getting file shell info
            Shfileinfo fileInfo = new Shfileinfo();

            SHGetFileInfo(
                _extension_,
                0,
                out fileInfo,
                Marshal.SizeOf(fileInfo),
                FileInfoFlags.ShgfiIcon | FileInfoFlags.ShgfiUsefileattributes | (FileInfoFlags)_size_);

            return(Icon.FromHandle(fileInfo.hIcon));
        }
        public static List <Icon> ExtractEx(string _fileName_, SystemIconSize _size_,
                                            int _firstIconIndex_, int _iconCount_)
        {
            List <Icon> iconList = new List <Icon>();

            switch (_size_)
            {
            case SystemIconSize.Large:
                ExtractEx(_fileName_, iconList, null, _firstIconIndex_, _iconCount_);
                break;

            case SystemIconSize.Small:
                ExtractEx(_fileName_, null, iconList, _firstIconIndex_, _iconCount_);
                break;

            default:
                throw new ArgumentOutOfRangeException("_size_");
            }

            return(iconList);
        }
Ejemplo n.º 23
0
            public static Icon IconFromExtensionShell(string extension, SystemIconSize size)
            {
                //add '.' if nessesry
                //if (extension[0] != '.') extension = '.' + extension;

                //temp struct for getting file shell info
                SHFILEINFO fileInfo = new SHFILEINFO();

                SHGetFileInfo(
                    extension,
                    0,
                    ref fileInfo,
                    (uint)Marshal.SizeOf(fileInfo),
                    (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)size);

                if (fileInfo.hIcon != IntPtr.Zero)
                {
                    return(Icon.FromHandle(fileInfo.hIcon));
                }

                return(null);
            }
Ejemplo n.º 24
0
        public static Icon IconFromExtension(string extension, SystemIconSize size)
        {
            Icon rv = null;

            string className = "Unknown";

            using (RegistryKey classKey =
                       Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + extension + @"\OpenWithProgids"))
            {
                if (null != classKey)
                {
                    className = classKey.GetValueNames().First();
                }
            }

            if (className.EndsWith("Folder") || extension == DirectoryExtension)
            {
                className = "Folder";
            }

            if (className.Equals("icofile"))
            {
                className = "Unknown";
            }

            try
            {
                rv = IconFromClassName(size, className);
            }
            catch { }

            if (null == rv)
            {
                rv = IconFromClassName(size, "Unknown");
            }

            return(rv);
        }
Ejemplo n.º 25
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 System.Drawing.Icon GetFileIcon(string name, SystemIconSize size, bool linkOverlay)
        {
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

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

            /* Check the size specified for return. */
            if (SystemIconSize.Small == size)
            {
                flags += Shell32.SHGFI_SMALLICON;
            }
            else
            {
                flags += Shell32.SHGFI_LARGEICON;
            }

            Shell32.SHGetFileInfo(name,
                                  Shell32.FILE_ATTRIBUTE_NORMAL,
                                  ref shfi,
                                  (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                  flags);

            if (shfi.hIcon != IntPtr.Zero)
            {
                // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
                System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
                User32.DestroyIcon(shfi.hIcon);                         // Cleanup
                return(icon);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 26
0
        public static Icon IconFromExtension(string extension, SystemIconSize size)
        {
            Icon rv = null;

            string className = "Unknown";
            using (RegistryKey classKey =
                Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + extension + @"\OpenWithProgids"))
            {
                if (null != classKey)
                {
                    className = classKey.GetValueNames().First();
                }
            }

            if (className.EndsWith("Folder") || extension == DirectoryExtension)
            {
                className = "Folder";
            }

            if (className.Equals("icofile"))
            {
                className = "Unknown";
            }

            try
            {
                rv = IconFromClassName(size, className);
            }
            catch { }

            if (null == rv)
            {
                rv = IconFromClassName(size, "Unknown");
            }

            return rv;
        }
Ejemplo n.º 27
0
        public static List<Icon> ExtractEx(string fileName, SystemIconSize size,
            int firstIconIndex, int iconCount)
        {
            List<Icon> iconList = new List<Icon>();

            switch (size)
            {
                case SystemIconSize.Large:
                    ExtractEx(fileName, iconList, null, firstIconIndex, iconCount);
                    break;

                case SystemIconSize.Small:
                    ExtractEx(fileName, null, iconList, firstIconIndex, iconCount);
                    break;

                default:
                    throw new ArgumentOutOfRangeException("size");
            }

            return iconList;
        }
Ejemplo n.º 28
0
        public static Icon IconFromExtension(string extension, SystemIconSize size)
        {
            RegistryKey currentUser = Registry.CurrentUser;
            var         className   = "Unknown";
            var         classKey    = currentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + extension + @"\OpenWithProgids");

            if (classKey != null)
            {
                className = classKey.GetValueNames().First();
            }
            RegistryKey Root = Registry.ClassesRoot;

            if (className.EndsWith("Folder"))
            {
                className = "Folder";
            }
            if (className.Equals("icofile"))
            {
                className = "Unknown";
            }

            RegistryKey ApplicationKey = Root.OpenSubKey(className);

            RegistryKey CurrentVer = null;

            try
            {
                CurrentVer = Root.OpenSubKey(ApplicationKey.OpenSubKey("CurVer").GetValue("").ToString());
            }
            catch (Exception) { }

            if (CurrentVer != null)
            {
                ApplicationKey = CurrentVer;
            }

            if (ApplicationKey == null)
            {
                ApplicationKey = Root.OpenSubKey("Unknown");
            }

            string IconLocation =
                ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();

            string[] IconPath = IconLocation.Split(',');


            IntPtr[] Large           = null;
            IntPtr[] Small           = null;
            int      iIconPathNumber = 0;

            if (IconPath.Length > 1)
            {
                iIconPathNumber = 1;
            }
            else
            {
                iIconPathNumber = 0;
            }


            if (IconPath[iIconPathNumber] == null)
            {
                IconPath[iIconPathNumber] = "0";
            }
            Large = new IntPtr[1];
            Small = new IntPtr[1];
            if (iIconPathNumber > 0)
            {
                ExtractIconEx(IconPath[0], Convert.ToInt16(IconPath[iIconPathNumber]), Large, Small, 1);
            }
            else
            {
                ExtractIconEx(IconPath[0], Convert.ToInt16(0), Large, Small, 1);
            }

            return(size == SystemIconSize.Large ? Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]));
        }
Ejemplo n.º 29
0
        public static Icon IconFromExtensionUsingRegistry(string extension, SystemIconSize size)
        {
            if (extension == null)
            {
                throw new ArgumentNullException(nameof(extension));
            }

            if (extension.Length == 0 || extension == ".")
            {
                throw new ArgumentException("Empty extension", nameof(extension));
            }

            if (extension[0] != '.')
            {
                extension = '.' + extension;
            }

            string iconLocation = GetExtensionIconStringFromRegistry(extension);
            if (iconLocation == null)
            {
                return null;
            }

            return ExtractFromRegistryString(iconLocation, size);
        }
Ejemplo n.º 30
0
            public static Icon IconFromExtensionShell(string extension, SystemIconSize size)
            {
                //add '.' if nessesry
                //if (extension[0] != '.') extension = '.' + extension;

                //temp struct for getting file shell info
                SHFILEINFO fileInfo = new SHFILEINFO();

                SHGetFileInfo(
                    extension,
                    0,
                    ref fileInfo,
                    (uint)Marshal.SizeOf(fileInfo),
                    (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)size);

                if (fileInfo.hIcon != IntPtr.Zero)
                {
                    return Icon.FromHandle(fileInfo.hIcon);
                }

                return null;
            }
Ejemplo n.º 31
0
        /// <summary>
        /// Used to access system folder icons.
        /// </summary>
        /// <param name="size">Specify large or small icons.</param>
        /// <param name="folderType">Specify open or closed FolderType.</param>
        /// <returns>System.Drawing.Icon</returns>
        public static System.Drawing.Icon GetFolderIcon( SystemIconSize size, FolderType folderType )
        {
            // Need to add size check, although errors generated at present!
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

            if (FolderType.Open == folderType)
            {
                flags += Shell32.SHGFI_OPENICON;
            }

            if (SystemIconSize.Small == size)
            {
                flags += Shell32.SHGFI_SMALLICON;
            }
            else
            {
                flags += Shell32.SHGFI_LARGEICON;
            }

            // Get the folder icon
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
            Shell32.SHGetFileInfo(	"dummy",  // Windows 7 needs a string passed to this regardless. Previous versions of windows could handle null
                Shell32.FILE_ATTRIBUTE_DIRECTORY,
                ref shfi,
                (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                flags );

            if (shfi.hIcon != IntPtr.Zero)
            {
                System.Drawing.Icon.FromHandle(shfi.hIcon);	// Load the icon from an HICON handle

                // Now clone the icon, so that it can be successfully stored in an ImageList
                System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();

                User32.DestroyIcon(shfi.hIcon);		// Cleanup
                return icon;
            }
            else
                return null;
        }
Ejemplo n.º 32
0
        public static Icon IconFromExtensionUsingRegistry(string extension, SystemIconSize size)
        {
            if (extension == null) throw new ArgumentNullException("extension");
            if (extension.Length == 0 || extension == ".") throw new ArgumentException("Empty extension", "extension");

            if (extension[0] != '.') extension = '.' + extension;

            var iconLocation = GetExtensionIconStringFromRegistry(extension);
            if (iconLocation == null) return null;

            return ExtractFromRegistryString(iconLocation, size);
        }
        public static List <Icon> Extract(string _fileName_, SystemIconSize _size_)
        {
            int iconCount = GetIconsCountInFile(_fileName_);

            return(ExtractEx(_fileName_, _size_, 0, iconCount));
        }
Ejemplo n.º 34
0
        public static Icon GetFolderIcon(SystemIconSize size, SystemFolderType systemFolderType)
        {
            var fileInfo = new SHFILEINFO();

            SHGetFileInfo(Environment.CurrentDirectory, (int) FILE_ATTRIBUTE_DIRECTORY, out fileInfo,
                          Marshal.SizeOf(fileInfo),
                          FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags) size |
                          (FileInfoFlags)systemFolderType);

            var fromHandle = Icon.FromHandle(fileInfo.hIcon).Clone();
            DestroyIcon(fileInfo.hIcon);
            return (Icon)fromHandle;
        }
Ejemplo n.º 35
0
        public static Icon IconFromExtension(string extension, SystemIconSize size)
        {
            if (extension == null) throw new ArgumentNullException("extension");
            if (extension.Length == 0 || extension == ".") throw new ArgumentException("Empty extension", "extension");

            if (extension[0] != '.') extension = '.' + extension;

            var fileInfo = new SHFILEINFO();
            SHGetFileInfo(extension, 0, out fileInfo, Marshal.SizeOf(fileInfo),
                FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (FileInfoFlags)size);

            return Icon.FromHandle(fileInfo.hIcon);
        }
Ejemplo n.º 36
0
        private static Icon IconFromClassName(SystemIconSize size, string className)
        {
            Icon rv = null;

            // TODO: using / Dispose for reg keys?
            RegistryKey root = Registry.ClassesRoot;
            RegistryKey applicationKey = root.OpenSubKey(className);
            RegistryKey currentVer = null;

            RegistryKey curVerSubKey = applicationKey.OpenSubKey("CurVer");
            if (null != curVerSubKey)
            {
                object curVerValue = curVerSubKey.GetValue(String.Empty);
                if (null != curVerValue)
                {
                    currentVer = root.OpenSubKey(curVerValue.ToString());
                }
            }

            if (null != currentVer)
            {
                applicationKey = currentVer;
            }

            if (null == applicationKey)
            {
                applicationKey = root.OpenSubKey("Unknown");
            }

            RegistryKey appKeySubKey = applicationKey.OpenSubKey("DefaultIcon");
            string iconLocation = null;
            if (null != appKeySubKey)
            {
                object appKeySubKeyValue = appKeySubKey.GetValue(String.Empty);
                if (null != appKeySubKeyValue)
                {
                    iconLocation = appKeySubKeyValue.ToString();
                }
            }

            if (null != iconLocation)
            {
                string[] iconPath = iconLocation.Split(',');
                int iIconPathNumber = iconPath.Length > 1 ? 1 : 0;
                if (null == iconPath[iIconPathNumber])
                {
                    iconPath[iIconPathNumber] = "0";
                }

                IntPtr[] large = new IntPtr[1];
                IntPtr[] small = new IntPtr[1];
                if (iIconPathNumber > 0)
                {
                    ExtractIconEx(iconPath[0], Convert.ToInt16(iconPath[iIconPathNumber]), large, small, 1);
                }
                else
                {
                    ExtractIconEx(iconPath[0], Convert.ToInt16(0), large, small, 1);
                }

                rv = size == SystemIconSize.Large ? Icon.FromHandle(large[0]) : Icon.FromHandle(small[0]);
            }

            return rv;
        }
Ejemplo n.º 37
0
 public static Icon DirectoryIcon(SystemIconSize size)
 {
     return IconFromExtension(DirectoryExtension, size);
 }
Ejemplo n.º 38
0
        private static Icon IconFromClassName(SystemIconSize size, string className)
        {
            Icon rv = null;

            var         root           = Registry.ClassesRoot;
            var         applicationKey = root.OpenSubKey(className);
            RegistryKey currentVer     = null;

            RegistryKey curVerSubKey = applicationKey.OpenSubKey("CurVer");

            if (null != curVerSubKey)
            {
                object curVerValue = curVerSubKey.GetValue(String.Empty);
                if (null != curVerValue)
                {
                    currentVer = root.OpenSubKey(curVerValue.ToString());
                }
            }

            if (null != currentVer)
            {
                applicationKey = currentVer;
            }

            if (null == applicationKey)
            {
                applicationKey = root.OpenSubKey("Unknown");
            }

            RegistryKey appKeySubKey = applicationKey.OpenSubKey("DefaultIcon");
            string      iconLocation = null;

            if (null != appKeySubKey)
            {
                object appKeySubKeyValue = appKeySubKey.GetValue(String.Empty);
                if (null != appKeySubKeyValue)
                {
                    iconLocation = appKeySubKeyValue.ToString();
                }
            }

            if (null != iconLocation)
            {
                string[] iconPath        = iconLocation.Split(',');
                int      iIconPathNumber = iconPath.Length > 1 ? 1 : 0;
                if (null == iconPath[iIconPathNumber])
                {
                    iconPath[iIconPathNumber] = "0";
                }

                IntPtr[] large = new IntPtr[1];
                IntPtr[] small = new IntPtr[1];
                if (iIconPathNumber > 0)
                {
                    ExtractIconEx(iconPath[0], Convert.ToInt16(iconPath[iIconPathNumber]), large, small, 1);
                }
                else
                {
                    ExtractIconEx(iconPath[0], Convert.ToInt16(0), large, small, 1);
                }

                rv = size == SystemIconSize.Large ? Icon.FromHandle(large[0]) : Icon.FromHandle(small[0]);
            }

            return(rv);
        }
Ejemplo n.º 39
0
 public static Icon ExtractFromRegistryString(string regString, SystemIconSize size)
 {
     string fileName;
     int index;
     ExtractInformationsFromRegistryString(regString, out fileName, out index);
     try
     {
         return ExtractOne(fileName, index, size);
     }
     catch (IconNotFoundException)
     {
         return null;
     }
 }
Ejemplo n.º 40
0
 public static Icon ExtractOne(string fileName, int index, SystemIconSize size)
 {
     try
     {
         List<Icon> iconList = ExtractEx(fileName, size, index, 1);
         return iconList[0];
     }
     catch (UnableToExtractIconsException e)
     {
         throw new IconNotFoundException(fileName, index, e);
     }
 }
Ejemplo n.º 41
0
        //this will look throw the registry
        //to find if the Extension have an icon.
        public static Icon IconFromExtension(string extension,
																								SystemIconSize size)
        {
            // Add the '.' to the extension if needed
            if (extension[0] != '.') extension = '.' + extension;

            //opens the registry for the wanted key.
            RegistryKey Root = Registry.ClassesRoot;
            RegistryKey ExtensionKey = Root.OpenSubKey(extension);
            ExtensionKey.GetValueNames();
            RegistryKey ApplicationKey =
                    Root.OpenSubKey(ExtensionKey.GetValue("").ToString());

            RegistryKey CurrentVer = null;
            try
            {
                CurrentVer = Root.OpenSubKey(ApplicationKey.OpenSubKey("CurVer").GetValue("").ToString());
            }
            catch
            {
                //current version not found... carry on without it?
            }

            if (CurrentVer != null)
                ApplicationKey = CurrentVer;

            //gets the name of the file that have the icon.
            string IconLocation =
                    ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();
            string[] IconPath = IconLocation.Split(',');

            IntPtr[] Large = null;
            IntPtr[] Small = null;
            int iIconPathNumber = 0;

            if (IconPath.Length > 1)
                iIconPathNumber = 1;
            else
                iIconPathNumber = 0;

            if (IconPath[iIconPathNumber] == null) IconPath[iIconPathNumber] = "0";
            Large = new IntPtr[1];
            Small = new IntPtr[1];

            //extracts the icon from the file.
            if (iIconPathNumber > 0)
            {
                ExtractIconEx(IconPath[0],
                        Convert.ToInt16(IconPath[iIconPathNumber]), Large, Small, 1);
            }
            else
            {
                ExtractIconEx(IconPath[0],
                        Convert.ToInt16(0), Large, Small, 1);
            }

            return size == SystemIconSize.Large ?
                    Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]);
        }
Ejemplo n.º 42
0
 public static List<Icon> Extract(string fileName, SystemIconSize size)
 {
     int iconCount = GetIconsCountInFile(fileName);
     return ExtractEx(fileName, size, 0, iconCount);
 }
Ejemplo n.º 43
0
        public static List <System.Drawing.Icon> Extract(string fileName, SystemIconSize size)
        {
            int iconCount = GetIconsCountInFile(fileName);

            return(ExtractEx(fileName, size, 0, iconCount));
        }
Ejemplo n.º 44
0
        //this will look throw the registry 
        //to find if the Extension have an icon.
        public static Icon IconFromExtension(string extension,
                                                SystemIconSize size)
        {
            // Add the '.' to the extension if needed
            if (extension[0] != '.') extension = '.' + extension;

            //opens the registry for the wanted key.
            RegistryKey Root = Registry.ClassesRoot;
            RegistryKey ExtensionKey = Root.OpenSubKey(extension);
            ExtensionKey.GetValueNames();
            RegistryKey ApplicationKey =
                Root.OpenSubKey(ExtensionKey.GetValue("").ToString());

            //gets the name of the file that have the icon.
            string IconLocation =
                ApplicationKey.OpenSubKey("DefaultIcon").GetValue("").ToString();
            string[] IconPath = IconLocation.Split(',');

            if (IconLocation.Split(',').Length < 2)
            {
                IconPath = new string[2];
                IconPath[0] = IconLocation.Split(',')[0];
                IconPath[1] = null;
            }

            if (IconPath[1] == null) IconPath[1] = "0";
            IntPtr[] Large = new IntPtr[1], Small = new IntPtr[1];

            //extracts the icon from the file.
            ExtractIconEx(IconPath[0],
                Convert.ToInt16(IconPath[1]), Large, Small, 1);
            return size == SystemIconSize.Large ?
                Icon.FromHandle(Large[0]) : Icon.FromHandle(Small[0]);
        }
Ejemplo n.º 45
0
 /// <summary>
 /// Gets default system icon and type description for a directory.
 /// </summary>
 /// <param name="iconSize">Returned icon size.</param>
 /// <returns></returns>
 public static FileIconAndType GetDefaultDirectoryIconAndType(SystemIconSize iconSize = SystemIconSize.Small) => GetFileIconAndType(DirectoryKey, FileAttributes.Directory, iconSize);
Ejemplo n.º 46
0
            /// <summary>
            /// Retrieves system icon and type description for specified file system path.
            /// </summary>
            /// <param name="path">File system path.</param>
            /// <param name="attr">File attributes, if null, file attributes will be read from path.</param>
            /// <param name="iconSize">Returned icon size.</param>
            /// <returns>File icon and type structure.</returns>
            public static FileIconAndType GetFileIconAndType(string path, FileAttributes?attr = null, SystemIconSize iconSize = SystemIconSize.Small)
            {
                if (path != null && path[1] == ':' && path.Length == 2)
                {
                    path += @"\";
                }
                var shFileInfo = new NativeMethods.SHFILEINFO();
                int cbFileInfo = Marshal.SizeOf(shFileInfo);
                var flags      = NativeMethods.SHGFI.TypeName;

                if (attr != null && path.Length > 3)
                {
                    flags |= NativeMethods.SHGFI.UseFileAttributes;
                }
                switch (iconSize)
                {
                case SystemIconSize.Small: flags |= NativeMethods.SHGFI.Icon | NativeMethods.SHGFI.SmallIcon; break;

                case SystemIconSize.Medium: flags |= NativeMethods.SHGFI.Icon; break;

                case SystemIconSize.Large: flags |= NativeMethods.SHGFI.Icon | NativeMethods.SHGFI.LargeIcon; break;
                }
                NativeMethods.SHGetFileInfo(path, (int)attr, out shFileInfo, (uint)cbFileInfo, flags);
                return(new FileIconAndType
                {
                    Icon = (shFileInfo.hIcon != IntPtr.Zero) ? GetImageFromHIcon(shFileInfo.hIcon) : null,
                    TypeDescription = shFileInfo.szTypeName
                });
            }
Ejemplo n.º 47
0
 public static Icon ExtractFromRegistryString(string regString, SystemIconSize size)
 {
     string fileName;
     int index;
     ExtractInformationsFromRegistryString(regString, out fileName, out index);
     return ExtractOne(fileName, index, size);
 }
Ejemplo n.º 48
0
 /// <summary>
 /// Gets default system icon and type description for a file.
 /// </summary>
 /// <param name="iconSize">Returned icon size.</param>
 /// <returns></returns>
 public static FileIconAndType GetDefaultFileIconAndType(SystemIconSize iconSize = SystemIconSize.Small) => GetFileIconAndType(FileKey, FileAttributes.Normal, iconSize);
Ejemplo n.º 49
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 System.Drawing.Icon GetFileIcon(string name, SystemIconSize size, bool linkOverlay)
        {
            Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

            if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY;

            /* Check the size specified for return. */
            if (SystemIconSize.Small == size)
            {
                flags += Shell32.SHGFI_SMALLICON ;
            }
            else
            {
                flags += Shell32.SHGFI_LARGEICON ;
            }

            Shell32.SHGetFileInfo(	name,
                Shell32.FILE_ATTRIBUTE_NORMAL,
                ref shfi,
                (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                flags );

            if (shfi.hIcon != IntPtr.Zero)
            {
                // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
                System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
                User32.DestroyIcon(shfi.hIcon);		// Cleanup
                return icon;
            }
            else
                return null;
        }