public static System.Drawing.Icon GetFileIconFromExtension(String _extension, IconSize _size) { String iconLocation = String.Empty; // Add the '.' to the extension if needed if (_extension[0] != '.') { _extension = String.Format(".{0}", _extension); } //opens the registry for the wanted key. RegistryKey registryKeyRoot = Registry.ClassesRoot; RegistryKey registryKeyFileType = registryKeyRoot.OpenSubKey(_extension); if (registryKeyFileType == null) { return(IconManager.GetUnkownFileTypeIcon(_size)); } //Gets the default value of this key that contains the information of file type. Object defaultValue = registryKeyFileType.GetValue(""); if (defaultValue == null) { return(IconManager.GetUnkownFileTypeIcon(_size)); } //Go to the key that specifies the default icon associates with this file type. String defaultIcon = String.Format("{0}\\DefaultIcon", defaultValue.ToString()); RegistryKey registryKeyFileIcon = registryKeyRoot.OpenSubKey(defaultIcon); if (registryKeyFileIcon != null) { //Get the file contains the icon and the index of the icon in that file. Object value = registryKeyFileIcon.GetValue(""); if (value != null) { // Clear all unnecessary " sign in the string to avoid error. iconLocation = value.ToString().Replace("\"", ""); } registryKeyFileIcon.Close(); } else { return(IconManager.GetUnkownFileTypeIcon(_size)); } registryKeyFileType.Close(); registryKeyRoot.Close(); 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) { API.ExtractIconEx(iconPath[0], Convert.ToInt16(iconPath[iIconPathNumber]), large, small, 1); } else { API.ExtractIconEx(iconPath[0], Convert.ToInt16(0), large, small, 1); } System.Drawing.Icon icon = null; try { switch (_size) { case IconSize.Small: icon = System.Drawing.Icon.FromHandle(small[0]); break; case IconSize.Large: icon = System.Drawing.Icon.FromHandle(large[0]); break; } } catch (Exception) { return(IconManager.GetUnkownFileTypeIcon(_size)); } return(icon); }
/// <summary> /// Generates the ImageSource which is used for folders /// </summary> /// <param name="_iconSize">The size of the icon</param> /// <param name="_folderType">The type of the folder icon</param> /// <returns>Retunrs the ImageSource of the folder icon</returns> private static ImageSource GetFolderImageSource(IconSize _iconSize, FolderType _folderType) { System.Drawing.Icon icon = IconManager.GetFolderIcon(_iconSize, _folderType); return(ShellIcon.toImageSource(icon)); }
public static ImageSource GetFileImageSourceFromExtension(String _extension, IconSize _size) { System.Drawing.Icon icon = IconManager.GetFileIconFromExtension(_extension, _size); return(ShellIcon.toImageSource(icon)); }