Để lấy danh sách các tệp trong một thư mục, hãy sử dụng SearchOptions.AllDirectories trong C #.
Đầu tiên, hãy đặt thư mục mà bạn muốn có tệp -
string[] myFiles = Directory.GetFiles("D:\\New\\", "*.*", SearchOption.AllDirectories); Sau đây là một ví dụ hiển thị các tệp từ thư mục được đề cập ở trên -
Ví dụ
using System;
using System.Linq;
using System.IO;
class Program {
static void Main() {
string[] myFiles = Directory.GetFiles("D:\\New\\", "*.*", SearchOption.AllDirectories);
foreach (string res in myFiles) {
Console.WriteLine(res);
}
}
} Đầu ra
Sau đây là kết quả đầu ra. Nó liệt kê tất cả các thư mục của thư mục -
D:\New\one.txt D:\New\two.html D:\New\nature.png