C # implements a class of file recursion under the target path

Source: Internet
Author: User
Recursion

Using System;
Using System.IO;
Using System.Collections;

Namespace Dsclub
{
/**////<summary>
Summary description of the dirlist.
</summary>
public class Dirlist
{
private string Strinitfilepath;
private bool Bfatchall;

       //constructor
         public dirlist ()
        {
             Bfatchall = false;
            Strinitfilepath = "c:\\";
       }
        public dirlist (String strFilePath)
         {
            bfatchall = false;
            Strinitfilepath = strFilePath;
       }

       //whether to recursively file all files
         public bool Recursionfiles
        {
             Get
             {
                 return bfatchall;
           }
            Set
             {
                 Bfatchall = value;
           }
       }

       //Get file Functions
         public ArrayList GetFiles ()
        {
             return GetFiles (Strinitfilepath, Bfatchall);
       }

        public static ArrayList GetFiles (String strpath, BOOL Resultsall)
        {
             ArrayList al  = new ArrayList ();
           //Determine if the path exists
             if (! Directory.Exists (strpath))
            {
                 Throw (new ApplicationException ("Access path" + strpath +) does not exist, or it is not a folder. "));
           }

            string[] Temp =  Directory.GetFiles (strpath);
            foreach (string afile in temp)
             {
                 al. ADD (Afile);
           }

If no files exist in this directory, the folder path is returned and identified with///
if (temp. Length = = 0)
{
Al. ADD ("///" + strpath);
}

if (Resultsall)
{
temp = directory.getdirectories (strpath);
foreach (string Adir in temp)
{
Al. AddRange (GetFiles (Adir, Resultsall));
}
}

Return al;
}

}
}

There should be a system file check, and users can not access the system folder judgment, but this project is not used, and do not want to use try block effect efficiency.

Or the thought of recursion!



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.