/// <summary> /// 获取后缀名的关联图标 /// </summary> /// <param name="stubPath"></param> /// <param name="large"></param> /// <returns></returns> public static Icon GetAssociatedIcon(string stubPath, bool large) { Shell32.SHFILEINFO info = new Shell32.SHFILEINFO(); int cbFileInfo = Marshal.SizeOf(info); uint flags; if (large) flags = Shell32.SHGFI_ICON | Shell32.SHGFI_LARGEICON | Shell32.SHGFI_USEFILEATTRIBUTES; else flags = Shell32.SHGFI_ICON | Shell32.SHGFI_SMALLICON | Shell32.SHGFI_USEFILEATTRIBUTES; Shell32.SHGetFileInfo(stubPath, 256, ref info, (uint)cbFileInfo, flags); return (Icon)Icon.FromHandle(info.hIcon); }
/// <summary> /// 获取后缀名的关联图标 /// </summary> /// <param name="stubPath"></param> /// <param name="large"></param> /// <returns></returns> public static Icon GetAssociatedIcon(string stubPath, bool large) { Shell32.SHFILEINFO info = new Shell32.SHFILEINFO(); int cbFileInfo = Marshal.SizeOf(info); uint flags; if (large) { flags = Shell32.SHGFI_ICON | Shell32.SHGFI_LARGEICON | Shell32.SHGFI_USEFILEATTRIBUTES; } else { flags = Shell32.SHGFI_ICON | Shell32.SHGFI_SMALLICON | Shell32.SHGFI_USEFILEATTRIBUTES; } Shell32.SHGetFileInfo(stubPath, 256, ref info, (uint)cbFileInfo, flags); return((Icon)Icon.FromHandle(info.hIcon)); }
public static string GetDisplayName(string name, bool isDirectory) { uint flags = Shell32.SHGFI_TYPENAME | Shell32.SHGFI_USEFILEATTRIBUTES; Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); uint fileType = isDirectory ? Shell32.FILE_ATTRIBUTE_DIRECTORY : Shell32.FILE_ATTRIBUTE_NORMAL; Shell32.SHGetFileInfo(name, fileType, ref shfi, (uint)Marshal.SizeOf(shfi), flags); return shfi.szTypeName; }
/// <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 Icon GetFolderIcon(IconSize 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 (IconSize.Small == size) { flags += Shell32.SHGFI_SMALLICON; } else { flags += Shell32.SHGFI_LARGEICON; } Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfo(null, Shell32.FILE_ATTRIBUTE_DIRECTORY, ref shfi, (uint)Marshal.SizeOf(shfi), 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 Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon(shfi.hIcon); // Cleanup return icon; }
/// <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) { uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY; if (IconSize.Small == size) { flags += Shell32.SHGFI_SMALLICON; } else { flags += Shell32.SHGFI_LARGEICON; } Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfo(name, Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags); // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon(shfi.hIcon); // Cleanup return icon; }