Ways to traverse folders

Source: Internet
Author: User

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

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.