private static extern System.IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref ShellFileInfo psfi, uint cbFileInfo, uint uFlags );
public static Icon GetIcon(string fullName, IconSize size) { Icon icon; string key = GetItemKey(fullName, size); if (!IconCache.TryGetValue(key, out icon)) { ShellFileInfo info = new ShellFileInfo(); uint flags = ShgfiSysiConIndex; if (fullName.IndexOf(":") == -1) { flags = flags | ShgfiUseFileAttributes; } if (size == IconSize.Small) { flags = flags | ShgfiIcon | ShgfiSmallIcon; } else { flags = flags | ShgfiIcon; } SHGetFileInfo(fullName, 0, ref info, (uint)Marshal.SizeOf(info), flags); icon = Icon.FromHandle(info.hIcon); IconCache.Add(key, icon); } return(icon); }
private static Icon GetFileIcon(string path, bool largeIcon, bool extensionOnly) { ShellFileInfo fileInfo = new ShellFileInfo(); int flags = FileIconFlags | ((largeIcon == true) ? SHGFI_LARGEICON : SHGFI_SMALLICON); if (extensionOnly == true) { flags |= SHGFI_USEFILEATTRIBUTES; } if (SHGetFileInfo(path, 0x80, ref fileInfo, Marshal.SizeOf(fileInfo), flags) != 0) { IntPtr hIcon = new IntPtr(fileInfo.IconHandle); if (hIcon != (IntPtr)0) { Icon myIcon = Icon.FromHandle(hIcon); DeleteObject(hIcon); return(myIcon); } } return(null); }
public static string GetFileType(string path) { ShellFileInfo fileInfo = new ShellFileInfo(); fileInfo.TypeName = new string(' ', 80); if (SHGetFileInfo(path, 0, ref fileInfo, Marshal.SizeOf(fileInfo), FileTypeFlags) != 0) { return(fileInfo.TypeName.Trim()); } return(""); }
/// <summary> /// Gets the directory or device icon. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> private static byte[] GetGenericIcon(string path, bool isDirectoryOrDevice) { const uint SHGFI_ICON = 0x000000100; const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; const uint SHGFI_OPENICON = 0x000000002; const uint SHGFI_LARGEICON = 0x000000000; const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010; // Get the folder icon byte[] icon = null; var shFileInfo = new ShellFileInfo(); unsafe { try { var flags = SHGFI_ICON | SHGFI_LARGEICON; if (isDirectoryOrDevice) { flags = flags | SHGFI_USEFILEATTRIBUTES | SHGFI_OPENICON; } var result = SHGetFileInfo( path, FILE_ATTRIBUTE_DIRECTORY, out shFileInfo, (uint)Marshal.SizeOf(shFileInfo), flags); if (result == IntPtr.Zero) { throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); } // Load the icon from an HICON handle Icon.FromHandle(shFileInfo.hIcon); icon = Icon.FromHandle(shFileInfo.hIcon).ToByteArray(); } finally { // Clean up, since this is unmanaged code DestroyIcon(shFileInfo.hIcon); } } return(icon); }
public static Icon GetIcon(string fileName, bool bigSize) { ShellFileInfo shellInfo = new ShellFileInfo(); uint iconSize = SHGFI_SMALLICON; if (bigSize) { iconSize = SHGFI_LARGEICON; } SHGetFileInfo(fileName, 0, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), SHGFI_ICON | iconSize); Icon copiedIcon = System.Drawing.Icon.FromHandle(shellInfo.iconHandle).Clone() as Icon; DestroyIcon(shellInfo.iconHandle); return copiedIcon; }
public static Icon GetIcon(string fileName, bool bigSize) { ShellFileInfo shellInfo = new ShellFileInfo(); uint iconSize = SHGFI_SMALLICON; if (bigSize) { iconSize = SHGFI_LARGEICON; } SHGetFileInfo(fileName, 0, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), SHGFI_ICON | iconSize); Icon copiedIcon = System.Drawing.Icon.FromHandle(shellInfo.iconHandle).Clone() as Icon; DestroyIcon(shellInfo.iconHandle); return(copiedIcon); }
public static FileinfoObj GetFileInfo(string path, ItemType type, Size iconSize) { var fileInfoObj = new FileinfoObj(); var fileInfo = new ShellFileInfo(); var size = (uint)Marshal.SizeOf(fileInfo); uint dwFileAttributes = Interop.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL | (uint)(type == ItemType.Folder ? FileAttribute.Directory : FileAttribute.File); uint uFlags = (uint)(Interop.SHGFI.SHGFI_TYPENAME | Interop.SHGFI.SHGFI_USEFILEATTRIBUTES | (uint)ShellAttribute.LargeIcon | (uint)ShellAttribute.SmallIcon | (uint)ShellAttribute.OpenIcon | (uint)(ShellAttribute.Icon | ShellAttribute.UseFileAttributes)); var result = Interop.SHGetFileInfo(path, dwFileAttributes, out fileInfo, size, uFlags); var size1 = (uint)Marshal.SizeOf(fileInfo); if (result == IntPtr.Zero) { throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); } try { var iconObj = (Icon)Icon.FromHandle(fileInfo.hIcon).Clone(); var iconImageSOurce = Imaging.CreateBitmapSourceFromHIcon(iconObj.Handle, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(iconSize.Width, iconSize.Height)); fileInfoObj.Icon = iconImageSOurce; fileInfoObj.FilePath = path; fileInfoObj.Type = fileInfo.szTypeName; return(fileInfoObj); } catch { throw; } finally { Interop.DestroyIcon(fileInfo.hIcon); } }
public MainForm() { InitializeComponent(); Text = string.Format("{0} v{1}", Text, new Version(Application.ProductVersion).Trim()); Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location); _profileManager = new ProfileManager(DbName); _profileManager.Load(); PopulateProfiles(); //image/aspect getters olvColFilename.ImageGetter = x => { var ext = ((ProcessedFile)x).Info.Extension; var icon = ShellInfo.GetShellIcon(ext); if (!fileIconList.Images.ContainsKey(ext)) { fileIconList.Images.Add(ext, icon); } return(ext); }; olvColFilename.AspectGetter = x => ((ProcessedFile)x).Info.Name; olvColFiletype.AspectGetter = x => { var file = ((ProcessedFile)x); var si = new ShellFileInfo(file.Info.Extension); var defaultName = string.Format("{0} File", file.Info.Extension.TrimStart(new[] { '.' }).ToUpper()); var fileType = si.FileType == defaultName?string.Format("{0} File", file.Profile.Name) : si.FileType; return(fileType); }; olvColProfile.AspectGetter = x => ((ProcessedFile)x).Profile.Name; olvColSize.AspectGetter = x => ((ProcessedFile)x).Info.Length; olvColModified.AspectGetter = x => ((ProcessedFile)x).Info.LastWriteTime; olvColTotalLines.AspectGetter = x => ((ProcessedFile)x).Stats.TotalLines; olvColEmptyLines.AspectGetter = x => ((ProcessedFile)x).Stats.EmptyLines; olvColCommentLInes.AspectGetter = x => ((ProcessedFile)x).Stats.CommentedLines; olvColCodeLines.AspectGetter = x => ((ProcessedFile)x).Stats.CodeLines; olvColDirectory.AspectGetter = x => ((ProcessedFile)x).Info.DirectoryName; }
/// <summary> /// lookup and return an icon from windows shell /// </summary> /// <param name="name">path to the file</param> /// <param name="size">large or small</param> /// <param name="linkOverlay">true to include the overlay link iconlet</param> /// <returns>requested icon</returns> public static System.Drawing.Icon GetFileIcon( string filePath, EnumIconSize size, bool addLinkOverlay) { EnumFileInfoFlags flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES; // add link overlay if requested if (addLinkOverlay) { flags |= EnumFileInfoFlags.LINKOVERLAY; } // set size if (size == EnumIconSize.Small) { flags |= EnumFileInfoFlags.SMALLICON; } else { flags |= EnumFileInfoFlags.LARGEICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo( filePath, conFILE_ATTRIBUTE_NORMAL, ref shellFileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return(icon); }
public static Icon GetIcon(string path, ItemType type, IconSize iconSize, ItemState state) { var attributes = (uint)(type == ItemType.Folder ? FileAttribute.Directory : FileAttribute.File); var flags = (uint)(ShellAttribute.Icon | ShellAttribute.UseFileAttributes); if (type == ItemType.Folder && state == ItemState.Open) { flags |= (uint)ShellAttribute.OpenIcon; } if (iconSize == IconSize.Small) { flags |= (uint)ShellAttribute.SmallIcon; } else { flags |= (uint)ShellAttribute.LargeIcon; } var fileInfo = new ShellFileInfo(); var size = (uint)Marshal.SizeOf(fileInfo); var result = Interop.SHGetFileInfo(path, attributes, out fileInfo, size, flags); if (result == IntPtr.Zero) { throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); } try { return((Icon)Icon.FromHandle(fileInfo.hIcon).Clone()); } catch { throw; } finally { _ = Interop.DestroyIcon(fileInfo.hIcon); } }
private Image GetSystemIcon(string fileName, bool isFolder) { // Taken from: // http://www.dotnetjunkies.com/WebLog/malio/archive/2004/10/04/27603.aspx var flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES | EnumFileInfoFlags.SMALLICON; //if (folderType == EnumFolderType.Open) // flags |= EnumFileInfoFlags.OPENICON; // flags |= EnumFileInfoFlags.LINKOVERLAY; var shellFileInfo = new ShellFileInfo(); uint fileAttribute; if (isFolder) { fileAttribute = conFILE_ATTRIBUTE_DIRECTORY; } else { fileAttribute = conFILE_ATTRIBUTE_NORMAL; } SHGetFileInfo( fileName, fileAttribute, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy var icon = (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return(icon.ToBitmap()); }
/// <summary> /// lookup and return an icon from windows shell /// </summary> /// <param name="folderPath">path to folder<param> /// <param name="size">large or small</param> /// <param name="folderType">open or closed</param> /// <returns>requested icon</returns> public static System.Drawing.Icon GetFolderIcon( string folderPath, EnumIconSize size, EnumFolderType folderType) { EnumFileInfoFlags flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES; if (folderType == EnumFolderType.Open) { flags |= EnumFileInfoFlags.OPENICON; } if (EnumIconSize.Small == size) { flags |= EnumFileInfoFlags.SMALLICON; } else { flags |= EnumFileInfoFlags.LARGEICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo( folderPath, conFILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return(icon); }
/// <summary> /// Gets an icon for a given folder /// </summary> /// <param name="path">Path to the folder</param> /// <param name="small">If true, gets the small version of the icon. true returns the large version</param> /// <param name="open">Gets the open version of the folder's icon</param> /// <returns>Returns the folder's icon</returns> public static Icon GetFolderIcon( string path, bool small, bool open ) { IconFlags flags = ( IconFlags.Icon | IconFlags.AddOverlays | IconFlags.UseFileAttributes ); if ( open ) { flags |= IconFlags.OpenIcon; } if ( small ) { flags |= IconFlags.SmallIcon; } ShellFileInfo shellFileInfo = new ShellFileInfo( ); SHGetFileInfo( path, FileAttributeDirectory, ref shellFileInfo, ( uint )Marshal.SizeOf( shellFileInfo ), ( uint )flags ); Icon icon = ( Icon )Icon.FromHandle( shellFileInfo.hIcon ).Clone( ); DestroyIcon( shellFileInfo.hIcon ); return icon; }
/// <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 TIcon GetFolderIcon <TIcon>(IconSize size, FolderType folderType) where TIcon : class { // Need to add size check, although errors generated at present! var flags = ShellGetFileInfoFlags.Icon | ShellGetFileInfoFlags.UseFileAttributes; if (FolderType.Open == folderType) { flags |= ShellGetFileInfoFlags.OpenIcon; } if (IconSize.Small == size) { flags |= ShellGetFileInfoFlags.SmallIcon; } else { flags |= ShellGetFileInfoFlags.LargeIcon; } // Get the folder icon var shellFileInfo = new ShellFileInfo(); SHGetFileInfo(null, FILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), flags); //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 try { return(IconHelper.IconHandleTo <TIcon>(shellFileInfo.IconHandle)); } finally { if (shellFileInfo.IconHandle != IntPtr.Zero) { // Cleanup User32Api.DestroyIcon(shellFileInfo.IconHandle); } } }
/// <summary> /// Gets an icon for a given file /// </summary> /// <param name="filePath">Path to the file</param> /// <param name="addLinkOverlay">If true, adds a link overlay to the icon</param> /// <param name="small">If true, gets the small version of the icon. true returns the large version</param> /// <returns>Returns the file's icon</returns> public static Icon GetFileIcon( string filePath, bool addLinkOverlay, bool small ) { IconFlags flags = ( IconFlags.Icon | IconFlags.AddOverlays | IconFlags.UseFileAttributes ); if ( addLinkOverlay ) { flags |= IconFlags.LinkOverlay; } if ( small ) { flags |= IconFlags.SmallIcon; } ShellFileInfo shellFileInfo = new ShellFileInfo( ); SHGetFileInfo( filePath, FileAttributeNormal, ref shellFileInfo, ( uint )Marshal.SizeOf( shellFileInfo ), ( uint )flags ); Icon icon = (Icon)Icon.FromHandle( shellFileInfo.hIcon ).Clone( ); DestroyIcon( shellFileInfo.hIcon ); return icon; }
public static Icon GetIcon(string path, ItemType type, IconSize size, ItemState state) { var flags = (uint)(Interop.SHGFI_ICON | Interop.SHGFI_USEFILEATTRIBUTES); var attribute = (uint)(object.Equals(type, ItemType.Folder) ? Interop.FILE_ATTRIBUTE_DIRECTORY : Interop.FILE_ATTRIBUTE_FILE); if (object.Equals(type, ItemType.Folder) && object.Equals(state, ItemState.Open)) { flags += Interop.SHGFI_OPENICON; } if (object.Equals(size, IconSize.Small)) { flags += Interop.SHGFI_SMALLICON; } else { flags += Interop.SHGFI_LARGEICON; } var shfi = new ShellFileInfo(); var res = Interop.SHGetFileInfo(path, attribute, out shfi, (uint)Marshal.SizeOf(shfi), flags); if (object.Equals(res, IntPtr.Zero)) { throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); } try { Icon.FromHandle(shfi.hIcon); return((Icon)Icon.FromHandle(shfi.hIcon).Clone()); } catch { throw; } finally { Interop.DestroyIcon(shfi.hIcon); } }
/// <summary> /// Returns an icon for a given file extension - indicated by the name parameter. /// See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx /// </summary> /// <param name="filename">Filename</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 TIcon GetFileExtensionIcon <TIcon>(string filename, IconSize size, bool linkOverlay) where TIcon : class { var shfi = new ShellFileInfo(); // UseFileAttributes makes it simulate, just gets the icon for the extension var flags = ShellGetFileInfoFlags.Icon | ShellGetFileInfoFlags.UseFileAttributes; if (linkOverlay) { flags |= ShellGetFileInfoFlags.LinkOverlay; } // Check the size specified for return. if (IconSize.Small == size) { flags |= ShellGetFileInfoFlags.SmallIcon; } else { flags |= ShellGetFileInfoFlags.LargeIcon; } SHGetFileInfo(Path.GetFileName(filename), FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags); // TODO: Fix bad practise for cleanup, and use generics to allow the user to specify if it's an icon/bitmap/-source // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly try { return(IconHelper.IconHandleTo <TIcon>(shfi.IconHandle)); } finally { if (shfi.IconHandle != IntPtr.Zero) { // Cleanup NativeIconMethods.DestroyIcon(shfi.IconHandle); } } }
/// <summary> /// Retrieve a file icon with specific path name /// </summary> public static Icon GetFileIcon(string filePath, IconSize size, bool addLinkOverlay) { FileInfoFlags flags = FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES; flags |= (size == IconSize.Small) ? FileInfoFlags.SHGFI_SMALLICON : FileInfoFlags.SHGFI_LARGEICON; if (addLinkOverlay) { flags |= FileInfoFlags.SHGFI_LINKOVERLAY; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo(filePath, FILE_ATTRIBUTE_NORMAL, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), (uint)flags); // Deep copy Icon icon = (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone(); // Release icon handle DestroyIcon(shellFileInfo.hIcon); return(icon); }
/// <summary> /// Retrieve a folder icon with specific path /// </summary> public static Icon GetFolderIcon(string folderPath, IconSize size, FolderType folderType) { FileInfoFlags flags = FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES; flags |= (size == IconSize.Small) ? FileInfoFlags.SHGFI_SMALLICON : FileInfoFlags.SHGFI_LARGEICON; if (folderType == FolderType.Opened) { flags |= FileInfoFlags.SHGFI_OPENICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo(folderPath, FILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint)Marshal.SizeOf(shellFileInfo), (uint)flags); // Deep copy Icon icon = (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone(); // Release icon handle DestroyIcon(shellFileInfo.hIcon); return(icon); }
private static extern IntPtr SHGetFileInfo(string path, uint fileAttributes, ref ShellFileInfo fileInfo, uint fileInfoSize, uint flags);
public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, out ShellFileInfo psfi, uint cbFileInfo, uint uFlags);
private static extern int SHGetFileInfo(string pszPath, int dwAttributes, ref ShellFileInfo psfi, int cbSizeFileInfo, int uFlags);
/// <summary> /// Retrieve a file icon with specific path name /// </summary> public static Icon GetFileIcon(string filePath, IconSize size, bool addLinkOverlay) { FileInfoFlags flags = FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES; flags |= (size == IconSize.Small) ? FileInfoFlags.SHGFI_SMALLICON : FileInfoFlags.SHGFI_LARGEICON; if (addLinkOverlay) { flags |= FileInfoFlags.SHGFI_LINKOVERLAY; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo(filePath, FILE_ATTRIBUTE_NORMAL, ref shellFileInfo, (uint) Marshal.SizeOf(shellFileInfo), (uint) flags); // Deep copy Icon icon = (Icon) Icon.FromHandle(shellFileInfo.hIcon).Clone(); // Release icon handle DestroyIcon(shellFileInfo.hIcon); return icon; }
public static extern IntPtr SHGetFileInfo(String path, UInt32 file_attributes, ref ShellFileInfo shell_file_info, UInt32 file_info_size, UInt32 flags);
/// <summary> /// Retrieve a folder icon with specific path /// </summary> public static Icon GetFolderIcon(string folderPath, IconSize size, FolderType folderType) { FileInfoFlags flags = FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES; flags |= (size == IconSize.Small) ? FileInfoFlags.SHGFI_SMALLICON : FileInfoFlags.SHGFI_LARGEICON; if (folderType == FolderType.Opened) { flags |= FileInfoFlags.SHGFI_OPENICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo(folderPath, FILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint) Marshal.SizeOf(shellFileInfo), (uint) flags); // Deep copy Icon icon = (Icon) Icon.FromHandle(shellFileInfo.hIcon).Clone(); // Release icon handle DestroyIcon(shellFileInfo.hIcon); return icon; }
public static string GetFileTypeName(string ext) { string tempFile = Path.GetTempPath() + Guid.NewGuid().ToString("N") + ext; try { File.WriteAllBytes(tempFile, new byte[1] { 0 }); ShellFileInfo shfileinfo = new ShellFileInfo(); SHGetFileInfo(tempFile, 0, ref shfileinfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfileinfo), (uint)EnumFileInfoFlags.TYPENAME); return shfileinfo.szTypeName; } finally { try { File.Delete(tempFile); } catch { } } }
public static System.Drawing.Icon GetFileIcon(string filePath, EnumIconSize size, bool addLinkOverlay) { EnumFileInfoFlags flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES; // add link overlay if requested if (addLinkOverlay) { flags |= EnumFileInfoFlags.LINKOVERLAY; } // set size if (size == EnumIconSize.Small) { flags |= EnumFileInfoFlags.SMALLICON; } else { flags |= EnumFileInfoFlags.LARGEICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo( filePath, conFILE_ATTRIBUTE_NORMAL, ref shellFileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return icon; }
protected override ImageSource Convert(FileSystemItemViewModel viewModel, Type targetType) { if (viewModel == null) { return(null); } var rm = viewModel.ResourceManager; var bytes = viewModel.IsUpDirectory ? null : viewModel.Thumbnail; if (bytes == null) { string icon; switch (viewModel.Type) { case ItemType.Directory: if (viewModel.IsUpDirectory) { icon = "up"; } else if (viewModel.Name == "0000000000000000") { icon = "games_folder"; } else if (viewModel.Model.RecognitionState == RecognitionState.PartiallyRecognized) { icon = "xbox_logo"; } else if (viewModel.IsRefreshing) { icon = "refresh_folder"; } else { icon = "folder"; } break; case ItemType.Link: icon = "reparse_point"; break; case ItemType.File: if (viewModel.IsCompressedFile) { icon = "package"; } else if (viewModel.IsIso) { icon = "iso"; } else if (viewModel.IsXex) { icon = "xex"; } else { ImageSource image = null; var extension = Path.GetExtension(viewModel.Name); if (!string.IsNullOrEmpty(extension)) { var key = ThumbnailSize + extension; if (SystemIconCache.ContainsKey(key)) { image = SystemIconCache[key]; } else { try { var shinfo = new ShellFileInfo(); var path = viewModel.Path; if (!File.Exists(path)) { path = key; //creating a temporary dummy file, i.e. 16.png using (File.Create(path)) {} } var size = ThumbnailSize == 16 ? (uint)1 : 0; IconExtensions.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), 0x100 | size); if (path != viewModel.Path) { File.Delete(path); } if (IntPtr.Zero != shinfo.hIcon) { image = Imaging.CreateBitmapSourceFromHIcon(shinfo.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } } catch { } SystemIconCache.Add(key, image); } if (image != null) { return(image); } } icon = "file"; } break; case ItemType.Drive: switch (viewModel.Model.DriveType) { case DriveType.CDRom: icon = "drive_cd"; break; case DriveType.Network: icon = "drive_network"; break; case DriveType.Removable: icon = "drive_flash"; break; default: icon = "drive"; break; } break; default: throw new NotSupportedException("Invalid item type: " + viewModel.Type); } bytes = rm.GetContentByteArray(string.Format("/Resources/Items/{0}x{0}/{1}.png", ThumbnailSize, icon)); } return(bytes[0] == 0 ? null : StfsPackageExtensions.GetBitmapFromByteArray(bytes)); }
public static extern IntPtr SHGetFileInfo(string path, uint attributes, out ShellFileInfo fileInfo, uint size, uint flags);
/// /// lookup and return an icon from windows shell /// /// "folderPath">path to folder /// "size">large or small /// "folderType">open or closed /// requested icon public static System.Drawing.Icon GetFolderIcon( string folderPath, EnumIconSize size, EnumFolderType folderType) { EnumFileInfoFlags flags = EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES; if (folderType == EnumFolderType.Open) { flags |= EnumFileInfoFlags.OPENICON; } if (EnumIconSize.Small == size) { flags |= EnumFileInfoFlags.SMALLICON; } else { flags |= EnumFileInfoFlags.LARGEICON; } ShellFileInfo shellFileInfo = new ShellFileInfo(); SHGetFileInfo( folderPath, conFILE_ATTRIBUTE_DIRECTORY, ref shellFileInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), (uint)flags); // deep copy System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone(); // release handle DestroyIcon(shellFileInfo.hIcon); return icon; }