In the process of File Manager, there is a very important feature implementation, that is, to obtain the file description of different file types, as well as the access to the file icon
Implementing this functionality requires invoking the Windows API, as well as the operation of the Registry (the code is read some online after writing a relatively simple and understandable code according to your own understanding)
Here's a look at the code:
Get icon public int Fileexticon (string typeext, FileInfo f) {if (!exticon.containskey (typeext) && Typee) according to the extension XT = ". exe") {RegistryKey RegRead = Registry.ClassesRoot.OpenSubKey (Typeext); if (RegRead = = null) {return 0;} String subkey = Regread.getvalue (""). ToString (); RegistryKey RegRead1 = Registry.ClassesRoot.OpenSubKey (subkey); if (RegRead1 = = null) {return 0;} RegistryKey subKey1 = Regread1.opensubkey ("DefaultIcon"); String DefaultIcon = Subkey1.getvalue (""). ToString (); string[] Deficon = Defaulticon.split (', '); Icon IC = Getextracticon (deficon[0], int. Parse (deficon[1])); TREEIMAGELIST.IMAGES.ADD (IC); Exticon.add (Typeext, i++); return 0; } else if (Typeext = = ". exe") {string fullPath = F.fullname; Icon IC = Getextracticon (fullpath,0); if (IC! = null) {TREEIMAGELIST.IMAGES.ADD (IC); Exticon.add (Typeext, i++); }} return 0; }
Here you need to refer to the Windows API
Gets the System icon [DllImport ("Shell32.dll")] public static extern int ExtractIcon (IntPtr h, string strx, int ii); Public Icon Getextracticon (string filename,int iIndex) { try { IntPtr hicon = (IntPtr) ExtractIcon ( This. Handle, FileName, iIndex); if (hicon! = IntPtr.Zero) { icon icon = icon.fromhandle (HICON); return icon; } } catch (Exception e) { MessageBox.Show (e.message, "Error Prompt", 0,messageboxicon.error); } return null; }
There is a problem not resolved, the File box folder icon display is not clear, I do not know is not the cause of the ListView
Please advise
If you have any questions or suggestions, please contact [email protected]
C # Similar to Windows Explorer-get file icon