Files in the recursive directory

Source: Internet
Author: User

As you know, it's easy to get a list of all the files and folders in the first level of a directory:
DirectoryInfo di=new DirectoryInfo (strbasedir);//strbasedir is the starting directory, absolute address
Directoryinfo[] Dia=di. GetDirectories ();//Get all sub-directories
Fileinfo[] Fia=di. GetFiles ();//Get all the files from the starting directory
What if you want to get all the files and directories (including all subdirectories) in a directory? Directory is a layer of layers, we can not predict the depth of a directory, only to obtain the parent node, it is possible to understand the child nodes, to solve this problem, only the concept of recursion.
So what is recursion? Please do not have C friends to read first, I am here to simply say my understanding: recursion is a method, in this method, again call itself this method, which describes the depth of the operation of a thing ...-no nonsense, look at the code:

Public ArrayList al=new ArrayList ();
I use ArrayList as a dynamic array, very useful
public void Getalldirlist (string strbasedir)
{
DirectoryInfo di=new DirectoryInfo (Strbasedir);
Directoryinfo[] Dia=di. GetDirectories ();
for (int i=0;i<dia.length;i++)

{
Al. ADD (Dia[i]. FullName);
Dia[i]. FullName is the absolute address of a subdirectory and records it in ArrayList
Getalldirlist (Dia[i]. FullName);
Note: recursion. People with logical thinking should be able to react.
}
}

Finally, how do I get all the directory information out of ArrayList? As follows:

for (int i=0;i<al. count;i++)
{
Textbox1.appendtext (Al[i]. ToString () + "");
TextBox1 is a container, copy my Code, pay attention to changing your own container.
}

Files in the recursive directory

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.