Phương thức EnumerateFile () được sử dụng trong C # để lấy tất cả các tệp. Sử dụng thuộc tính AllDirectories để đệ quy thông qua các thư mục -
Directory.EnumerateFiles(@"D:\NEW", "*.*", SearchOption.AllDirectories)
Để lấy danh sách các tệp trong một thư mục, hãy sử dụng SearchOptions.AllDirectories trong C # như được hiển thị ở trên.
Hãy để chúng tôi xem cách thực hiện -
Ví dụ
using System;
using System.IO;
namespace Demo {
class Program {
static void Main(string[] args) {
foreach (string allFiles is Directory.EnumerateFiles(@"D:\NEW","*.*",SearchOption.AllDirectories)) {
Console.WriteLine(allFiles);
}
}
}
} Đầu ra
Sau đây là kết quả -
D:\NEW\my.txt D:\NEW\amit.html D:\NEW\tutorials\java\a.java