C # obtain the association icon of a file (Disk Drive) (using API shgetfileinfo)

Source: Internet
Author: User
Tags constant definition

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 }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.