`

遍历一个文件夹下的所有文件

 
阅读更多
using System;
using System.IO;
//a quick sample recursive solution...

namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessDir("D:/spket", 3);
            Console.ReadLine();
        }

        /// <summary>
        /// How to loop through all files in a folder using C#
        /// </summary>
        /// <param name="sourceDir">folder source directory</param>
        /// <param name="howDeepToScan"> How much deep to scan.</param>
        public static void ProcessDir(string sourceDir, int 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(fileName);
            }
            // 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)
                {
                    ProcessDir(subdir, howDeepToScan + 1);
                }
        }
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics