"Go" C # ListView instance: File icon display
Description: This example displays the files in the catalog in the form's ListView control and defines a variety of view views. An extract of the icon data is implemented by invoking the Win32 library function .
Main program: Large icons:
List:
More information:
Form1.cs:
Public partial class Form1:form {fileinfolist fileList; Public Form1 () {InitializeComponent (); private void Load file Toolstripmenuitem_click (object sender, EventArgs e) {FolderBrowserDialog dlg = new FolderBrowserDialog (); if (dlg. ShowDialog () = = DialogResult.OK) {string[] Filespath = Directory.GetFiles (dlg. SelectedPath); FileList = new Fileinfolist (Filespath); Initlistview (); }} private void Initlistview () {listView1.Items.Clear (); This.listView1.BeginUpdate (); foreach (Fileinfowithicon file in filelist.list) {ListViewItem item = new ListViewItem (); Item. Text = File.fileInfo.Name.Split ('. ') [0]; Item. ImageIndex = File.iconindex; Item. SubItems.Add (File.fileInfo.LastWriteTime.ToString ()); Item. SubItems. ADD (File.fileInfo.Extension.Replace (".", "")); Item. SubItems.Add (String. Format (("{0:n0}"), file.fileInfo.Length); LISTVIEW1.ITEMS.ADD (item); } listview1.largeimagelist = Filelist.imagelistlargeicon; Listview1.smallimagelist = Filelist.imagelistsmallicon; Listview1.show (); This.listView1.EndUpdate (); } private void Large icon Toolstripmenuitem_click (object sender, EventArgs e) {Listview1.view = View.lar Geicon; } private void Small icon Toolstripmenuitem_click (object sender, EventArgs e) {Listview1.view = View.sma Llicon; private void tile Toolstripmenuitem_click (object sender, EventArgs e) {Listview1.view = View.tile ; } private void list Toolstripmenuitem_click (object sender, EventArgs e) {Listview1.view = View.list ; } private void Details Toolstripmenuitem_click (object sender, EventArgs e) {listview1.view = View.Details; } }
FileInfoList.cs: Description: Storage primarily for background data
Class Fileinfolist {public list<fileinfowithicon> List; Public ImageList Imagelistlargeicon; Public ImageList Imagelistsmallicon; <summary>///////To obtain the generated file information according to the file path and extract the file icon///</summary>//<param name= "Filespath" &G T;</param> public fileinfolist (string[] filespath) {list = new List<fileinfowithicon> ;(); Imagelistlargeicon = new ImageList (); Imagelistlargeicon.imagesize = new Size (32, 32); Imagelistsmallicon = new ImageList (); Imagelistsmallicon.imagesize = new Size (16, 16); foreach (string path in Filespath) {Fileinfowithicon file = new Fileinfowithicon (path); IMAGELISTLARGEICON.IMAGES.ADD (File.largeicon); IMAGELISTSMALLICON.IMAGES.ADD (File.smallicon); File.iconindex = imagelistlargeicon.images.count-1; List. ADD (file); } }} class Fileinfowithicon {public FileInfo FileInfo; Public Icon LargeIcon; Public Icon SmallIcon; public int iconindex; Public Fileinfowithicon (string path) {fileInfo = new fileInfo (path); LargeIcon = Getsystemicon.geticonbyfilename (path, true); if (LargeIcon = = null) LargeIcon = Getsystemicon.geticonbyfiletype (Path.getextension (Path), true); SmallIcon = Getsystemicon.geticonbyfilename (path, false); if (SmallIcon = = null) SmallIcon = Getsystemicon.geticonbyfiletype (Path.getextension (Path), false); } }
Getsystemicon: Description: Defines two ways to get the icon, extracted from the file and from the system resources associated with the file.
public static class Getsystemicon {//<summary>/////////////////or NULL if the specified file does not exist. </summary>//<param name= "fileName" > File path </param>//<param name= "IsLarge" > whether to return Back to Large Icons </param>///<returns></returns> public static icon Geticonbyfilename (String fileName, BOOL IsLarge = True) {int[] Phiconlarge = new Int[1]; int[] Phiconsmall = new Int[1]; FileName Icon index Win32.extracticonex (filename, 0, Phiconlarge, Phiconsmall, 1); IntPtr iconhnd = new IntPtr (IsLarge phiconlarge[0]: phiconsmall[0]); if (iconhnd.tostring () = = "0") return null; Return Icon.fromhandle (ICONHND); }///<summary>///The icon associated with it is returned based on the file name extension (for example:. *). If you do not use "." The icon for the folder is returned at the beginning. </summary>//<param name= "FileType" > File extension </param>//<param NAMe= "IsLarge" > whether to return large icons </param>//<returns></returns> public static icon Geticonbyfi Letype (string fileType, bool IsLarge) {if (FileType = = NULL | | filetype.equals (string. EMPTY)) return null; RegistryKey regversion = null; string regfiletype = null; string regiconstring = null; String systemdirectory = environment.systemdirectory + "\ \"; if (filetype[0] = = '. ') {//Read the file type information in the system registry Regversion = Registry.ClassesRoot.OpenSubKey (FileType, false); if (regversion! = null) {Regfiletype = Regversion.getvalue ("") as String; Regversion.close (); Regversion = Registry.ClassesRoot.OpenSubKey (Regfiletype + @ "\defaulticon", false); if (regversion! = null) {regiconstring = Regversion.getvalue ("") as StrinG Regversion.close (); }} if (regiconstring = = null) {//Not read to file type registration information, specified as unknown file type The icon regiconstring = systemdirectory + "shell32.dll,0"; }} else {//directly specified as folder icon regiconstring = Systemdirectory + "shell32.dll,3"; } string[] Fileicon = Regiconstring.split (new char[] {', '}); if (fileicon.length! = 2) {//The plotting registered in the system registry cannot be extracted directly, then the generic icon for the executable file is returned Fileicon = new St Ring[] {systemdirectory + "Shell32.dll", "2"}; } Icon Resulticon = null; try {//Call API Method Read Icon int[] Phiconlarge = new Int[1]; int[] Phiconsmall = new Int[1]; UINT count = Win32.extracticonex (Fileicon[0], Int32.Parse (fileicon[1]), Phiconlarge, Phiconsmall, 1); IntPtr iconhnd = new IntPtr (IsLarge phiconlarge[0]: phiconsmall[0]); Resulticon = Icon.fromhandle (ICONHND); } catch {} return Resulticon; }}///<summary>///define the API method called///</summary> class Win32 {[DllImport ("Shell 32.dll ")] public static extern uint Extracticonex (string lpszfile, int niconindex, int[] phiconlarge, int[] Phicons Mall, uint nicons); }
"Go" C # ListView instance: File icon display