This is a demo of C # calling the system API shgetfileinfo. It is also a reply to a netizen. first look at it:
The shgetfileinfo API can obtain detailed information about the specified object. For details, refer to the description of this API on msdn.
This get association icon can get the icon of the disk partition, get the icon of a specific type of file, or get the icon of a specified file. The following code is provided:
/// <Summary> // structure for saving the file information /// </Summary> // [structlayout (layoutkind. sequential, charset = charset. auto)] struct shfileinfo {public intptr hicon; Public int iicon; Public uint dwattributes; [financialas (unmanagedtype. byvaltstr, sizeconst = 260)] Public String szdisplayname; [financialas (unmanagedtype. byvaltstr, sizeconst = 80)] Public String sztypename;} class nativemethods {[dllimport ("shell32.dll", entrypoint = "shgetfileinfo", setlasterror = true, charset = charset. auto)] public static extern intptr shgetfileinfo (string pszpath, uint struct, ref shfileinfo psfi, uint cbfileinfo, uint uflags); [dllimport ("user32.dll", entrypoint = "destroyicon")] public static extern int destroyicon (intptr hicon); # The constant definition of the region API parameter public const uint shgfi_icon = 0x100; Public const uint shgfi_largeicon = 0x0; // large icon 32 × 32 public const uint shgfi_smallicon = 0x1; // small icon 16 × 16 public const uint shgfi_usefileattributes = 0x10; # endregion} // <summary> // obtain the file type association icon /// </Summary> // <Param name = "FILENAME"> file type Extension or the absolute path of the file </param> /// <Param name = "islargeicon"> whether to return the large icon </param> /// <returns> the obtained icon </ returns> static icon getIcon (string filename, bool islargeicon) {shfileinfo shfi = new shfileinfo (); intptr Hi; If (islargeicon) HI = nativemethods. shgetfileinfo (filename, 0, ref shfi, (uint) Marshal. sizeof (shfi), nativemethods. shgfi_icon | nativemethods. shgfi_usefileattributes | nativemethods. shgfi_largeicon); else HI = nativemethods. shgetfileinfo (filename, 0, ref shfi, (uint) Marshal. sizeof (shfi), nativemethods. shgfi_icon | nativemethods. shgfi_usefileattributes | nativemethods. shgfi_smallicon); icon = icon. fromhandle (shfi. hicon ). clone () as icon; nativemethods. destroyicon (shfi. hicon); // release resource return icon;} private void linklabel1_linkclicked (Object sender, linklabellinkclickedeventargs e) {help. showHelp (this, "http://www.zu14.cn");} private void btngeticon_click (Object sender, eventargs e) {using (Graphics G = This. pbsmallicon. creategraphics () {G. clear (this. pbsmallicon. backcolor); // clear the background color of picturebox. To draw a transparent icon G. drawicon (getIcon (this. tbfileext. text, false), 1, 1); // draw a small icon} using (Graphics G = This. pblargeicon. creategraphics () {G. clear (this. pblargeicon. backcolor); // clear the background color of picturebox. To draw a transparent icon G. drawicon (getIcon (this. tbfileext. text, true), 1, 1); // draw a small icon }}