How can I perform full recursive directory & file scan?

后端 未结 6 1184
囚心锁ツ
囚心锁ツ 2020-12-11 01:38

here is my code:

    private static void TreeScan(string sDir)
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            forea         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 02:18

    There are some problems with your code. For one, the reason you never saw the files from the root folder is because your recursed before doing and file reads. Try this:

    public static void Main()
    {
        TreeScan(@"C:\someFolder");
    }
    
    private static void TreeScan(string sDir)
    {
        foreach (string f in Directory.GetFiles(sDir))
            Console.WriteLine("File: " + f); // or some other file processing
    
        foreach (string d in Directory.GetDirectories(sDir))
            TreeScan(d); // recursive call to get files of directory
    }
    

提交回复
热议问题