遍历文件夹及文件

遍历文件夹及文件

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessDir(@"F:\Todo\MVC", 1);
            Console.ReadLine();
        }
        const int HowDeepToScan = 3;
        ArrayList dirList = new ArrayList();
        static int numTopFolder;
        static string rootDir = @"F:\Todo\MVC";
        static int numFile;
        public static void ProcessDir(string sourceDir, int recursionLvl)
        {
            if (numFile == 0)
            {
                numTopFolder = Directory.GetDirectories(rootDir).Length;
            }
            if (recursionLvl <= HowDeepToScan)
            {
                // Process the list of files found in the directory. 
                string[] fileEntries = Directory.GetFiles(sourceDir);
                foreach (string fileName in fileEntries)
                {
                    // do something with fileName
                    Console.WriteLine("file:"+fileName);
                    if(numFile % 3 == 0 & numFile!=0)
                    {
                        Console.WriteLine("Update on batch1");
                    }
                    numFile++;
                }

                // Recurse into subdirectories of this directory.
                string[] subdirEntries = Directory.GetDirectories(sourceDir);
                foreach (string subdir in subdirEntries)
                    // Do not iterate through reparse points
                    if ((File.GetAttributes(subdir) &
                         FileAttributes.ReparsePoint) !=
                             FileAttributes.ReparsePoint)
                    {
                        Console.WriteLine(subdir.ToString() + ";recursionLvl:" + recursionLvl);
                        if (recursionLvl == 1)
                        {
                            numTopFolder--;
                        }
                        ProcessDir(subdir, recursionLvl + 1);
                    }
            }            
           
            if (numTopFolder==0 & string.Equals(rootDir,sourceDir))
            {
                Console.WriteLine("Update on batch2" + ";numFile:"+numFile);
            }
        }
    }
}

 

 

posted @ 2012-12-12 13:19  风影极光  阅读(277)  评论(0编辑  收藏  举报