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 ~