C # Get the file icon,

Source: Internet
Author: User

C # Get the file icon,

Today, I suddenly came up with a question: How can I get the associated icon of a file? So I searched the internet. The following is a summary:

First, clarify the problem: Get the associated icon of a file or the display icon of a type of file.

I searched the internet and found that there are still many methods, but there are not many methods to obtain using C. I chose a method to use the. Net library.

Classes used:

System. Drawing. Icon, which is located in the System. Drawing namespace.

Specific Method:

Static Method in System. Drawing. Icon class: public static Icon ExtractAssociatedIcon (string filePath)

This method receives a file path and returns an Icon-type object. This object is the Icon object of the specified file.

This Icon class provides the Save method, which can write the Icon file to the stream and Save it to the hard disk file.

Of course, it is also possible to display in the program. The Icon class provides the ToBitmap method to convert the Icon file to Bitmap. Bitmap can be displayed in PictureBox.

What should I do if I want to retrieve the file icon Based on the suffix? I thought of a stupid way. View code ~

String fileName = "tmp." + houzhuiming;
File. Create (fileName). Close ();
Image img = System. Drawing. Icon. ExtractAssociatedIcon (fileName). ToBitmap ();
File. Delete (fileName );

In this way, you can create a new file with the specified suffix name and delete the file directly after obtaining the icon to get the icon with the specified suffix name. Xi ~

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.