IT story

디렉토리 + 하위 디렉토리의 모든 파일 및 디렉토리 나열

hot-time 2020. 9. 8. 21:59
반응형

디렉토리 + 하위 디렉토리의 모든 파일 및 디렉토리 나열


디렉토리 및 해당 디렉토리의 하위 디렉토리에 포함 된 모든 파일과 디렉토리를 나열하고 싶습니다. 디렉토리로 C : \를 선택하면 프로그램은 액세스 권한이있는 하드 드라이브에있는 모든 파일과 폴더의 모든 이름을 가져옵니다.

목록은 다음과 같을 수 있습니다.

fd \ 1.txt
fd \ 2.txt
fd \ a \
fd \ b \
fd \ a \ 1.txt
fd \ a \ 2.txt
fd \ a \ a \
fd \ a \ b \
fd \ b \ 1.txt
fd \ b \ 2.txt
fd \ b \ a
fd \ b \ b
fd \ a \ a \ 1.txt
fd \ a \ a \ a \
fd \ a \ b \ 1.txt
fd \ a \ b \ a
fd \ b \ a \ 1.txt
fd \ b \ a \ a \
fd \ b \ b \ 1.txt
fd \ b \ b \ a

string[] allfiles = Directory.GetFiles("path/to/dir", "*.*", SearchOption.AllDirectories);

*.*파일과 일치시킬 패턴은 어디에 있습니까?

디렉토리도 필요하면 다음과 같이 갈 수 있습니다.

 foreach (var file in allfiles){
     FileInfo info = new FileInfo(file);
 // Do something with the Folder or just add them to a list via nameoflist.add();
 }

Directory.GetFileSystemEntries.NET 4.0+에 존재하며 파일과 디렉토리를 모두 반환합니다. 그렇게 부르십시오.

string[] entries = Directory.GetFileSystemEntries(path, "*", SearchOption.AllDirectories);

액세스 권한이없는 하위 디렉터리의 내용을 나열하려는 시도 (UnauthorizedAccessException)에는 대처할 수 없지만 필요에 따라 충분할 수 있습니다.


GetDirectoriesGetFiles메서드를 사용하여 폴더와 파일을 가져옵니다.

를 사용하여 하위 폴더의 폴더와 파일도 가져옵니다.SearchOption AllDirectories


public static void DirectorySearch(string dir)
{
    try
    {
        foreach (string f in Directory.GetFiles(dir))
        {
            Console.WriteLine(Path.GetFileName(f));
        }
        foreach (string d in Directory.GetDirectories(dir))
        {
            Console.WriteLine(Path.GetFileName(d));
            DirectorySearch(d);
        }
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

GetFiles방법은 파일 목록을 반환하지만 디렉토리는 반환하지 않습니다. 질문의 목록은 결과에 폴더도 포함되어야한다는 메시지를 표시합니다. 더 많은 사용자 정의 목록을 원하면 호출 GetFilesGetDirectories재귀 적으로 시도 할 수 있습니다 . 이 시도:

List<string> AllFiles = new List<string>();
void ParsePath(string path)
{
    string[] SubDirs = Directory.GetDirectories(path);
    AllFiles.AddRange(SubDirs);
    AllFiles.AddRange(Directory.GetFiles(path));
    foreach (string subdir in SubDirs)
        ParsePath(subdir);
}

팁 : 특정 속성을 확인해야하는 경우 FileInfoDirectoryInfo클래스를 사용할 수 있습니다 .


핸들을 반환하는 FindFirstFile을 사용하고 FindNextFile을 호출하는 함수를 재귀 적으로 계산할 수 있습니다. 참조 된 구조가 alternativeName, lastTmeCreated, modified, attributes 등과 같은 다양한 데이터로 채워지기 때문에 이것은 좋은 접근 방식입니다.

그러나 .net 프레임 워크를 사용하면 관리되지 않는 영역으로 들어가야합니다.


디렉토리 트리 내부의 하위 폴더에 액세스 할 수없는 경우 Directory.GetFiles는 수신 문자열 []에 null 값이 발생하는 예외를 중지하고 throw합니다.

여기 에서이 답변을 참조하십시오 https://stackoverflow.com/a/38959208/6310707

루프 내에서 예외를 관리하고 전체 폴더가 탐색 될 때까지 계속 작업합니다.


논리적이고 정돈 된 방법 :

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace DirLister
{
class Program
{
    public static void Main(string[] args)
    {
        //with reflection I get the directory from where this program is running, thus listing all files from there and all subdirectories
        string[] st = FindFileDir(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
        using ( StreamWriter sw = new StreamWriter("listing.txt", false ) )
        {
            foreach(string s in st)
            {
                //I write what I found in a text file
                sw.WriteLine(s);
            }
        }
    }

    private static string[] FindFileDir(string beginpath)
    {
        List<string> findlist = new List<string>();

        /* I begin a recursion, following the order:
         * - Insert all the files in the current directory with the recursion
         * - Insert all subdirectories in the list and rebegin the recursion from there until the end
         */
        RecurseFind( beginpath, findlist );

        return findlist.ToArray();
    }

    private static void RecurseFind( string path, List<string> list )
    {
        string[] fl = Directory.GetFiles(path);
        string[] dl = Directory.GetDirectories(path);
        if ( fl.Length>0 || dl.Length>0 )
        {
            //I begin with the files, and store all of them in the list
            foreach(string s in fl)
                list.Add(s);
            //I then add the directory and recurse that directory, the process will repeat until there are no more files and directories to recurse
            foreach(string s in dl)
            {
                list.Add(s);
                RecurseFind(s, list);
            }
        }
    }
}
}

다음 예제는 예외를 처리하는 디렉토리 트리에서 파일과 하위 폴더를 나열 하는 가장 빠른 (병렬화되지 않은) 방법입니다. SearchOption.AllDirectories를 사용하여 Directory.EnumerateDirectories를 사용하여 모든 디렉토리를 열거하는 것이 더 빠르지 만 UnauthorizedAccessException 또는 PathTooLongException이 발생하면이 메서드가 실패합니다.

LIFO (last in first out) 스택이고 재귀를 사용하지 않는 일반 스택 컬렉션 유형을 사용합니다. 에서 https://msdn.microsoft.com/en-us/library/bb513869.aspx , 모든 하위 디렉토리 및 파일을 열거하고 그 예외를 효과적으로 처리 할 수 있습니다.

    public class StackBasedIteration
{
    static void Main(string[] args)
    {
        // Specify the starting folder on the command line, or in 
        // Visual Studio in the Project > Properties > Debug pane.
        TraverseTree(args[0]);

        Console.WriteLine("Press any key");
        Console.ReadKey();
    }

    public static void TraverseTree(string root)
    {
        // Data structure to hold names of subfolders to be
        // examined for files.
        Stack<string> dirs = new Stack<string>(20);

        if (!System.IO.Directory.Exists(root))
        {
            throw new ArgumentException();
        }
        dirs.Push(root);

        while (dirs.Count > 0)
        {
            string currentDir = dirs.Pop();
            string[] subDirs;
            try
            {
                subDirs = System.IO.Directory.EnumerateDirectories(currentDir); //TopDirectoryOnly
            }
            // An UnauthorizedAccessException exception will be thrown if we do not have
            // discovery permission on a folder or file. It may or may not be acceptable 
            // to ignore the exception and continue enumerating the remaining files and 
            // folders. It is also possible (but unlikely) that a DirectoryNotFound exception 
            // will be raised. This will happen if currentDir has been deleted by
            // another application or thread after our call to Directory.Exists. The 
            // choice of which exceptions to catch depends entirely on the specific task 
            // you are intending to perform and also on how much you know with certainty 
            // about the systems on which this code will run.
            catch (UnauthorizedAccessException e)
            {                    
                Console.WriteLine(e.Message);
                continue;
            }
            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
                continue;
            }

            string[] files = null;
            try
            {
                files = System.IO.Directory.EnumerateFiles(currentDir);
            }

            catch (UnauthorizedAccessException e)
            {

                Console.WriteLine(e.Message);
                continue;
            }

            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
                continue;
            }
            // Perform the required action on each file here.
            // Modify this block to perform your required task.
            foreach (string file in files)
            {
                try
                {
                    // Perform whatever action is required in your scenario.
                    System.IO.FileInfo fi = new System.IO.FileInfo(file);
                    Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
                }
                catch (System.IO.FileNotFoundException e)
                {
                    // If file was deleted by a separate application
                    //  or thread since the call to TraverseTree()
                    // then just continue.
                    Console.WriteLine(e.Message);
                    continue;
                }
                catch (UnauthorizedAccessException e)
                {                    
                    Console.WriteLine(e.Message);
                    continue;
                }
            }

            // Push the subdirectories onto the stack for traversal.
            // This could also be done before handing the files.
            foreach (string str in subDirs)
                dirs.Push(str);
        }
    }
}

하나는 종료 용이고 다른 하나는 시작 용으로 두 개의 버튼이있는 양식에 다음 코드를 사용합니다. 폴더 브라우저 대화 상자 및 파일 저장 대화 상자. 코드는 아래에 나열되어 있으며 내 시스템 Windows10 (64)에서 작동합니다.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Directory_List
{

    public partial class Form1 : Form
    {
        public string MyPath = "";
        public string MyFileName = "";
        public string str = "";

        public Form1()
        {
            InitializeComponent();
        }    
        private void cmdQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }    
        private void cmdGetDirectory_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            MyPath = folderBrowserDialog1.SelectedPath;    
            saveFileDialog1.ShowDialog();
            MyFileName = saveFileDialog1.FileName;    
            str = "Folder = " + MyPath + "\r\n\r\n\r\n";    
            DirectorySearch(MyPath);    
            var result = MessageBox.Show("Directory saved to Disk!", "", MessageBoxButtons.OK);
                Application.Exit();    
        }    
        public void DirectorySearch(string dir)
        {
                try
            {
                foreach (string f in Directory.GetFiles(dir))
                {
                    str = str + dir + "\\" + (Path.GetFileName(f)) + "\r\n";
                }    
                foreach (string d in Directory.GetDirectories(dir, "*"))
                {

                    DirectorySearch(d);
                }
                        System.IO.File.WriteAllText(MyFileName, str);

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

문자열 목록 만들기

    public static List<string> HTMLFiles = new List<string>();

 private void Form1_Load(object sender, EventArgs e)
        {

     HTMLFiles.AddRange(Directory.GetFiles(@"C:\DataBase", "*.txt"));
            foreach (var item in HTMLFiles)
            {
                MessageBox.Show(item);
            }

}

using System.IO;
using System.Text;
string[] filePaths = Directory.GetFiles(@"path", "*.*", SearchOption.AllDirectories);

조금 간단하고 느리지 만 작동합니다 !! 파일 경로를 제공하지 않으면 기본적으로 "fixPath"를 사용하십시오. 이것은 단지 예일뿐입니다 .... 원하는 올바른 fileType을 검색 할 수 있습니다. "temporaryFileList가 검색된 파일 목록이기 때문에 목록 이름을 선택할 때 실수했습니다. 그러니 계속하십시오 .... 그리고 "errorList"는 스스로를 말합니다.

 static public void Search(string path, string fileType, List<string> temporaryFileList, List<string> errorList)
    {

        List<string> temporaryDirectories = new List<string>();

        //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);
    }

참고 URL : https://stackoverflow.com/questions/12332451/list-all-files-and-directories-in-a-directory-subdirectories

반응형