Icon getIcon(bool large) { // Get icon index and path: int iconIndex = 0; StringBuilder iconPath = new StringBuilder(260, 260); if (linkA == null) { linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } else { linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } string iconFile = iconPath.ToString(); // If there are no details set for the icon, then we must use // the shell to get the icon for the target: if (iconFile.Length == 0) { // Use the FileIcon object to get the icon: FileIcon.SHGetFileInfoConstants flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES; if (large) { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON; } else { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON; } FileIcon fileIcon = new FileIcon(Target, flags); return fileIcon.ShellIcon; } else { // Use ExtractIconEx to get the icon: IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; int iconCount = 0; if (large) { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, hIconEx, null, 1); } else { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, null, hIconEx, 1); } // If success then return as a GDI+ object Icon icon = null; if (hIconEx[0] != IntPtr.Zero) { icon = Icon.FromHandle(hIconEx[0]); //UnManagedMethods.DestroyIcon(hIconEx[0]); } return icon; } }
Icon getIcon(bool large) { // Get icon index and path: int iconIndex = 0; StringBuilder iconPath = new StringBuilder(260, 260); if (linkA == null) { linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } else { linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } string iconFile = iconPath.ToString(); // If there are no details set for the icon, then we must use // the shell to get the icon for the target: if (iconFile.Length == 0) { // Use the FileIcon object to get the icon: FileIcon.SHGetFileInfoConstants flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES; if (large) { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON; } else { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON; } FileIcon fileIcon = new FileIcon(Target, flags); return(fileIcon.ShellIcon); } else { // Use ExtractIconEx to get the icon: IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; int iconCount = 0; if (large) { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, hIconEx, null, 1); } else { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, null, hIconEx, 1); } // If success then return as a GDI+ object Icon icon = null; if (hIconEx[0] != IntPtr.Zero) { icon = Icon.FromHandle(hIconEx[0]); //UnManagedMethods.DestroyIcon(hIconEx[0]); } return(icon); } }
/// <summary> /// Constructs a new instance of the FileIcon class /// and retrieves the information specified in the /// flags. /// </summary> /// <param name="fileName">The filename to get information /// for</param> /// <param name="flags">The flags to use when extracting the /// icon and other shell information.</param> public FileIcon(string fileName, FileIcon.SHGetFileInfoConstants flags) { this.fileName = fileName; this.flags = flags; GetInfo(); }