This article will introduce to you how to obtain the association icon of a file? The summary is now as follows:
First, clarify the problem: get the associated icon of a file or the display icon of a certain type of file.
After searching online, I found that there are still many methods, but there are not many methods to obtain using C#. I chose a method that uses the .Net library.
Classes used:
, located in the namespace.
Specific methods:
Static method in class: public static Icon ExtractAssociatedIcon(string filePath)
This method receives a file path and returns an object of type Icon. This object is both the Icon icon object of the specified file.
This Icon class provides a Save method that allows you to write Icon files to a stream and then save them to a hard disk file.
Of course, it is also OK to display it in the program. The Icon class provides the ToBitmap method, which can convert the Icon file into a Bitmap. Bitmap can be displayed in PictureBox.
What should I do if I want to get the file icon based on the suffix name? I thought of a stupid method. Look at the code ~
string fileName = "tmp." + houz*g; (fileName).Close(); Image img = (fileName).ToBitmap(); (fileName);
In this way, create a new file with a specified suffix name, and delete it directly after obtaining the icon, and you can get the icon with a specified file suffix name. Hehehe~
Below is a code introduction to C# obtain file association icon
using System; using ; using ; using ; using ; using Microsoft.Win32; namespace { class SystemIcon { [StructLayout(, CharSet = )] public struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(, SizeConst = 260)] public string szDisplayName; [MarshalAs(, SizeConst = 80)] public string szTypeName; } [DllImport("", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = )] public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags); [DllImport("", EntryPoint = "DestroyIcon")] public static extern int DestroyIcon(IntPtr hIcon); #region API parameters constant definitionpublic enum FileInfoFlags : uint { SHGFI_ICON = 0x000000100, // get icon SHGFI_DISPLAYNAME = 0x000000200, // get display name SHGFI_TYPENAME = 0x000000400, // get type name SHGFI_ATTRIBUTES = 0x000000800, // get attributes SHGFI_ICONLOCATION = 0x000001000, // get icon location SHGFI_EXETYPE = 0x000002000, // return exe type SHGFI_SYSICONINDEX = 0x000004000, // get system icon index SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon SHGFI_SELECTED = 0x000010000, // show icon in selected state SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes SHGFI_LARGEICON = 0x000000000, // get large icon SHGFI_SMALLICON = 0x000000001, // get small icon SHGFI_OPENICON = 0x000000002, // get open icon SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon SHGFI_PIDL = 0x000000008, // pszPath is a pidl SHGFI_USEFILEATTRIBUTES = 0x000000010, // use passed dwFileAttribute SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay } public enum FileAttributeFlags : uint { FILE_ATTRIBUTE_READONLY = 0x00000001, FILE_ATTRIBUTE_HIDDEN = 0x00000002, FILE_ATTRIBUTE_SYSTEM = 0x00000004, FILE_ATTRIBUTE_DIRECTORY = 0x00000010, FILE_ATTRIBUTE_ARCHIVE = 0x00000020, FILE_ATTRIBUTE_DEVICE = 0x00000040, FILE_ATTRIBUTE_NORMAL = 0x00000080, FILE_ATTRIBUTE_TEMPORARY = 0x00000100, FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200, FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400, FILE_ATTRIBUTE_COMPRESSED = 0x00000800, FILE_ATTRIBUTE_OFFLINE = 0x00001000, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000, FILE_ATTRIBUTE_ENCRYPTED = 0x00004000 } #endregion /// <summary> /// Get the associated icon for file type/// </summary> /// <param name="fileName">Extension of file type or absolute path to file</param>/// <param name="isLargeIcon">Whether to return the large icon</param>/// <returns>Getted icon</returns>public static Icon GetIcon(string fileName, bool isLargeIcon) { SHFILEINFO shfi = new SHFILEINFO(); IntPtr hI; if (isLargeIcon) hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)(shfi), (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)FileInfoFlags.SHGFI_LARGEICON); else hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)(shfi), (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)FileInfoFlags.SHGFI_SMALLICON); Icon icon = ().Clone() as Icon; DestroyIcon(); //Release resourcesreturn icon; } /// <summary> /// Get folder icon/// </summary> /// <returns>Icon</returns>public static Icon GetDirectoryIcon(bool isLargeIcon) { SHFILEINFO _SHFILEINFO = new SHFILEINFO(); IntPtr _IconIntPtr; if (isLargeIcon) { _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)(_SHFILEINFO), ((uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_LARGEICON)); } else { _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)(_SHFILEINFO), ((uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_SMALLICON)); } if (_IconIntPtr.Equals()) return null; Icon _Icon = (_SHFILEINFO.hIcon); return _Icon; } } }