To traverse only the subfolders and root folders below the file directory method:
DirectoryInfo Thefolder =NewDirectoryInfo (@"e:\ Work \work");//action classes for filesfileinfo[] Filerootinfo= Thefolder.getfiles ();//returns the file of the current root directory foreach(FileInfo Nextfileinchfilerootinfo) { This. LISTBOX1.ITEMS.ADD (Nextfile.name);//traversing Files} directoryinfo[] Dirinfo= Thefolder.getdirectories ();//returns the current file subdirectory//Traverse Folder foreach(DirectoryInfo Nextfolderinchdirinfo) { //This.listBox2.Items.Add (nextfolder.name);fileinfo[] FileInfo = Nextfolder.getfiles ();//returns the file of the current file subdirectory foreach(FileInfo NextfileinchFileInfo)//traversing Files This. LISTBOX2.ITEMS.ADD (Nextfile.fullname); }
Recursively traverse all the folders below the folder's subclass directory:
PublicArrayList Al =NewArrayList (); //I used the ArrayList as a dynamic array . Public voidGetalldirlist (stringStrbasedir) {DirectoryInfo di=NewDirectoryInfo (Strbasedir); Directoryinfo[] DiA=di. GetDirectories (); for(inti =0; i < dia.length; i++) {al. ADD (Dia[i]. FullName); //Dia[i]. FullName is the absolute address of a subdirectory and records it in ArrayListgetalldirlist (Dia[i]. FullName); //Note: recursion. People with logical thinking should be able to react. } for(inti =0; I < al. Count; i++) { This. LISTBOX1.ITEMS.ADD (Al[i]. ToString () +" "); //TextBox1 is a container, copy my Code, pay attention to changing your own container. } }
Ways to traverse folders