Share Lấy tất cả file trong thư mục trong lập trình C#

## Nhận tất cả các tệp trong thư mục trong lập trình C#

Trong hướng dẫn này, bạn sẽ học cách lấy tất cả các tệp trong một thư mục trong C#.Chúng tôi sẽ bao gồm hai phương thức: sử dụng phương thức `thư mục.getfiles ()` và sử dụng phương thức `DirectoryInfo.getFiles ()`.

### Sử dụng phương thức `thư mục.getfiles ()`

Phương thức `thư mục.getfiles ()` Trả về một mảng các chuỗi chứa tên của tất cả các tệp trong một thư mục được chỉ định.Cú pháp của phương thức `thư mục.getfiles ()` như sau:

`` `C#
Chuỗi [] getfiles (đường dẫn chuỗi, chuỗi searchPotype)
`` `

Tham số `` Path` chỉ định thư mục để lấy các tệp từ.Tham số `searchPotyern` chỉ định một mẫu ký tự đại diện có thể được sử dụng để lọc các tệp được trả về.Ví dụ: mã sau có tất cả các tệp trong thư mục hiện tại có phần mở rộng `.txt`:

`` `C#
Chuỗi [] tập tin = thư mục.getfiles (".", "*.txt");
`` `

### Sử dụng phương thức `thư mụcInfo.getFiles ()`

Phương thức `thư mụcInfo.getFiles ()` trả về một mảng của các đối tượng `fileInfo` đại diện cho tất cả các tệp trong một thư mục được chỉ định.Cú pháp của phương thức `thư mụcInfo.getFiles ()` như sau:

`` `C#
FileInfo [] getfiles ()
`` `

Mã sau đây có tất cả các tệp trong thư mục hiện tại có phần mở rộng `.txt`:

`` `C#
Thư mục thư mục = thư mục mới (".");
FileInfo [] files = thư mục.getfiles ("*. Txt");
`` `

### Bạn nên sử dụng phương pháp nào?

Trong hầu hết các trường hợp, bạn có thể sử dụng phương thức `thư mục.getfiles ()` hoặc phương thức `DirectoryInfo.getFiles ()`.Tuy nhiên, có một số khác biệt nhỏ giữa hai phương pháp.

Phương thức `thư mụcĐiều này có nghĩa là nếu bạn cần truy cập các thuộc tính tệp, chẳng hạn như kích thước tệp hoặc ngày tạo, bạn sẽ cần chuyển các đối tượng `fileInfo` vào loại thích hợp.

Phương thức `thư mục.getfiles ()` cũng hỗ trợ mẫu ký tự đại diện, trong khi phương thức `DirectoryInfo.getFiles ()` không.Điều này có nghĩa là bạn có thể sử dụng phương thức `Directory.getFiles ()` để lọc các tệp được trả về.

Nhìn chung, phương thức `Directory.getFiles ()` linh hoạt hơn phương thức `DirectoryInfo.getFiles ()`.Tuy nhiên, phương thức `DirectoryInfo.getFiles ()` sẽ hiệu quả hơn nếu bạn chỉ cần truy cập tên tệp.

### Mã ví dụ

Mã sau đây có tất cả các tệp trong thư mục hiện tại có phần mở rộng `.txt` và in tên tệp vào bảng điều khiển:

`` `C#
Chuỗi [] tập tin = thư mục.getfiles (".", "*.txt");

foreach (tệp chuỗi trong tệp)
{
Console.WriteLine (Tệp);
}
`` `

### hashtags

* #C#
* Quản lý #file
* #Danh mục
* #WildCard Mẫu
* #nộp thông tin
=======================================
## Get all files in the folder in C# programming

In this tutorial, you will learn how to get all files in a folder in C#. We will cover two methods: using the `Directory.GetFiles()` method and using the `DirectoryInfo.GetFiles()` method.

### Using the `Directory.GetFiles()` method

The `Directory.GetFiles()` method returns an array of strings that contains the names of all the files in a specified directory. The syntax of the `Directory.GetFiles()` method is as follows:

```c#
string[] GetFiles(string path, string searchPattern)
```

The `path` parameter specifies the directory to get the files from. The `searchPattern` parameter specifies a wildcard pattern that can be used to filter the files that are returned. For example, the following code gets all the files in the current directory that have the `.txt` extension:

```c#
string[] files = Directory.GetFiles(".", "*.txt");
```

### Using the `DirectoryInfo.GetFiles()` method

The `DirectoryInfo.GetFiles()` method returns an array of `FileInfo` objects that represents all the files in a specified directory. The syntax of the `DirectoryInfo.GetFiles()` method is as follows:

```c#
FileInfo[] GetFiles()
```

The following code gets all the files in the current directory that have the `.txt` extension:

```c#
DirectoryInfo directory = new DirectoryInfo(".");
FileInfo[] files = directory.GetFiles("*.txt");
```

### Which method should you use?

In most cases, you can use either the `Directory.GetFiles()` method or the `DirectoryInfo.GetFiles()` method. However, there are some slight differences between the two methods.

The `Directory.GetFiles()` method returns an array of strings, while the `DirectoryInfo.GetFiles()` method returns an array of `FileInfo` objects. This means that if you need to access the file attributes, such as the file size or creation date, you will need to cast the `FileInfo` objects to the appropriate type.

The `Directory.GetFiles()` method also supports a wildcard pattern, while the `DirectoryInfo.GetFiles()` method does not. This means that you can use the `Directory.GetFiles()` method to filter the files that are returned.

Overall, the `Directory.GetFiles()` method is more flexible than the `DirectoryInfo.GetFiles()` method. However, the `DirectoryInfo.GetFiles()` method is more efficient if you only need to access the file names.

### Example code

The following code gets all the files in the current directory that have the `.txt` extension and prints the file names to the console:

```c#
string[] files = Directory.GetFiles(".", "*.txt");

foreach (string file in files)
{
Console.WriteLine(file);
}
```

### Hashtags

* #C#
* #file management
* #directory
* #WildCard pattern
* #FileInfo
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top