ITeye博客

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

20444465 · 2011-07-29 11:50 · C# · Interview · 编程语言 · Markdown

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(); } /// /// How to loop through all files in a folder using C# /// /// folder source directory /// How much deep to scan. 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); } } } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。