List all files and directories in a directory + subdirectories

前端 未结 15 1885
陌清茗
陌清茗 2020-12-13 01:46

I want to list every file and directory contained in a directory and subdirectories of that directory. If I chose C:\\ as the directory, the program would get every name of

15条回答
  •  温柔的废话
    2020-12-13 02:21

    A littlebit simple and slowly but working!! if you do not give a filepath basicly use the "fixPath" this is just example.... you can search the correct fileType what you want, i did a mistake when i chosen the list name because the "temporaryFileList is the searched file list so carry on that.... and the "errorList" is speaks for itself

     static public void Search(string path, string fileType, List temporaryFileList, List errorList)
        {
    
            List temporaryDirectories = new List();
    
            //string fix = @"C:\Users\" + Environment.UserName + @"\";
            string fix = @"C:\";
            string folders = "";
            //Alap útvonal megadása 
            if (path.Length != 0)
            { folders = path; }
            else { path = fix; }
    
            int j = 0;
            int equals = 0;
            bool end = true;
    
            do
            {
    
                equals = j;
                int k = 0;
    
                try
                {
    
                    int foldersNumber = 
                    Directory.GetDirectories(folders).Count();
                    int fileNumber = Directory.GetFiles(folders).Count();
    
                    if ((foldersNumber != 0 || fileNumber != 0) && equals == j)
                    {
    
                        for (int i = k; k < 
                        Directory.GetDirectories(folders).Length;)
                        {
    
                 temporaryDirectories.Add(Directory.GetDirectories(folders)[k]);
                            k++;
                        }
    
                        if (temporaryDirectories.Count == j)
                        {
                            end = false;
                            break;
                        }
                        foreach (string files in Directory.GetFiles(folders))
                        {
                            if (files != string.Empty)
                            {
                                if (fileType.Length == 0)
                                {
                                    temporaryDirectories.Add(files);
                                }
                                else
                                {
    
                                    if (files.Contains(fileType))
                                    {
                                        temporaryDirectories.Add(files);
    
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
    
                    }
    
                    equals++;
    
                    for (int i = j; i < temporaryDirectories.Count;)
                    {
                        folders = temporaryDirectories[i];
                        j++;
                        break;
                    }
    
                }
                catch (Exception ex)
                {
                    errorList.Add(folders);
    
                    for (int i = j; i < temporaryDirectories.Count;)
                    {
                        folders = temporaryDirectories[i];
                        j++;
                        break;
                    }
                }
            } while (end);
        }
    

提交回复
热议问题