Share c# đọc file text

## Cách đọc tệp văn bản trong C#

Đọc một tệp văn bản trong C# là một nhiệm vụ tương đối đơn giản.Bạn có thể sử dụng lớp `streamreader` để mở tệp và đọc từng dòng nội dung của nó.

Để đọc một tệp văn bản, trước tiên bạn cần tạo một đối tượng `streamreader`.Bạn có thể làm điều này bằng cách gọi hàm tạo `streamreader` và chuyển đường dẫn đến tệp dưới dạng tham số.

`` `C#
StreamReader Reader = new StreamReader ("myfile.txt");
`` `

Khi bạn đã tạo một đối tượng `streamreader`, bạn có thể sử dụng phương thức` readline () `để đọc dòng văn bản tiếp theo từ tệp.Phương thức `readline ()` trả về một `chuỗi` chứa dòng văn bản đã được đọc.

`` `C#
chuỗi dòng = reader.Readline ();
`` `

Bạn có thể tiếp tục gọi phương thức `readline ()` cho đến khi kết thúc tệp.Khi kết thúc tệp, phương thức `readline ()` sẽ trả về `null`.

Để đóng tệp, bạn có thể gọi phương thức `đóng ()` trên đối tượng `streamreader`.

`` `C#
độc giả.close ();
`` `

Dưới đây là một ví dụ về một chương trình hoàn chỉnh đọc tệp văn bản và in nội dung của nó vào bảng điều khiển:

`` `C#
sử dụng hệ thống;
sử dụng System.io;

Chương trình lớp học
{
static void main (String [] args)
{
// Tạo một đối tượng StreamReader cho tệp.
StreamReader Reader = new StreamReader ("myfile.txt");

// Đọc các dòng văn bản từ tệp.
while (đúng)
{
// Đọc dòng văn bản tiếp theo từ tệp.
chuỗi dòng = reader.Readline ();

// Nếu kết thúc của tệp đã đạt được, hãy thoát ra khỏi vòng lặp.
if (line == null)
{
phá vỡ;
}

// In dòng văn bản vào bảng điều khiển.
Console.WriteLine (dòng);
}

// Đóng tệp.
độc giả.close ();
}
}
`` `

### hashtags

* #C#
* #file I/O
* Các tệp #Text
* #Reading Tệp
* #Programming
=======================================
## How to Read a Text File in C#

Reading a text file in C# is a relatively simple task. You can use the `StreamReader` class to open a file and read its contents line by line.

To read a text file, you first need to create a `StreamReader` object. You can do this by calling the `StreamReader` constructor and passing the path to the file as a parameter.

```c#
StreamReader reader = new StreamReader("myfile.txt");
```

Once you have created a `StreamReader` object, you can use the `ReadLine()` method to read the next line of text from the file. The `ReadLine()` method returns a `string` containing the line of text that was read.

```c#
string line = reader.ReadLine();
```

You can continue to call the `ReadLine()` method until the end of the file is reached. When the end of the file is reached, the `ReadLine()` method will return `null`.

To close the file, you can call the `Close()` method on the `StreamReader` object.

```c#
reader.Close();
```

Here is an example of a complete program that reads a text file and prints its contents to the console:

```c#
using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
// Create a StreamReader object for the file.
StreamReader reader = new StreamReader("myfile.txt");

// Read the lines of text from the file.
while (true)
{
// Read the next line of text from the file.
string line = reader.ReadLine();

// If the end of the file has been reached, break out of the loop.
if (line == null)
{
break;
}

// Print the line of text to the console.
Console.WriteLine(line);
}

// Close the file.
reader.Close();
}
}
```

### Hashtags

* #C#
* #file I/O
* #Text Files
* #Reading Files
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top