Method to get all files within folder and subfolders that will return a list

前端 未结 8 1171
别跟我提以往
别跟我提以往 2020-12-04 17:30

I have a method that will iterate through a folder and all of its subfolders and get a list of the file paths. However, I could only figure out how to create it and add the

8条回答
  •  无人及你
    2020-12-04 18:14

    Try this
    class Program { static void Main(string[] args) {

            getfiles get = new getfiles();
            List files =  get.GetAllFiles(@"D:\Rishi");
    
            foreach(string f in files)
            {
                Console.WriteLine(f);
            }
    
    
            Console.Read();
        }
    
    
    }
    
    class getfiles
    {
        public List GetAllFiles(string sDirt)
        {
            List files = new List();
    
            try
            {
                foreach (string file in Directory.GetFiles(sDirt))
                {
                    files.Add(file);
                }
                foreach (string fl in Directory.GetDirectories(sDirt))
                {
                    files.AddRange(GetAllFiles(fl));
                }
            }
            catch (Exception ex)
            {
    
                Console.WriteLine(ex.Message);
            }
    
    
    
            return files;
        }
    }
    

提交回复
热议问题