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

前端 未结 8 1137
别跟我提以往
别跟我提以往 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:09

    This is for anyone that is trying to get a list of all files in a folder and its sub-folders and save it in a text document. Below is the full code including the “using” statements, “namespace”, “class”, “methods” etc. I tried commenting as much as possible throughout the code so you could understand what each part is doing. This will create a text document that contains a list of all files in all folders and sub-folders of any given root folder. After all, what good is a list (like in Console.WriteLine) if you can’t do something with it. Here I have created a folder on the C drive called “Folder1” and created a folder inside that one called “Folder2”. Next I filled folder2 with a bunch of files, folders and files and folders within those folders. This example code will get all the files and create a list in a text document and place that text document in Folder1. Caution: you shouldn’t save the text document to Folder2 (the folder you are reading from), that would be just bad practice. Always save it to another folder.
    I hope this helps someone down the line.

    using System;
    using System.IO;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            public static void Main(string[] args)
            {
                // Create a header for your text file
                string[] HeaderA = { "****** List of Files ******" };
                System.IO.File.WriteAllLines(@"c:\Folder1\ListOfFiles.txt", HeaderA);
    
                // Get all files from a folder and all its sub-folders. Here we are getting all files in the folder
                // named "Folder2" that is in "Folder1" on the C: drive. Notice the use of the 'forward and back slash'.
                string[] arrayA = Directory.GetFiles(@"c:\Folder1/Folder2", "*.*", SearchOption.AllDirectories);
                {
                    //Now that we have a list of files, write them to a text file.
                    WriteAllLines(@"c:\Folder1\ListOfFiles.txt", arrayA);
                }
    
                // Now, append the header and list to the text file.
                using (System.IO.StreamWriter file =
                    new System.IO.StreamWriter(@"c:\Folder1\ListOfFiles.txt"))
                {
                    // First - call the header 
                    foreach (string line in HeaderA)
                    {
                        file.WriteLine(line);
                    }
    
                    file.WriteLine(); // This line just puts a blank space between the header and list of files. 
    
    
                    // Now, call teh list of files.
                    foreach (string name in arrayA)
                    {
                        file.WriteLine(name);
                    }
    
                }
              }
    
    
            // These are just the "throw new exception" calls that are needed when converting the array's to strings. 
    
            // This one is for the Header.
            private static void WriteAllLines(string v, string file)
            {
                //throw new NotImplementedException();
            }
    
            // And this one is for the list of files. 
            private static void WriteAllLines(string v, string[] arrayA)
            {
                //throw new NotImplementedException();
            }
    
    
    
        }
    }
    

提交回复
热议问题