🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 在 C# 中读取文本文件 > 原文: [https://zetcode.com/csharp/readtext/](https://zetcode.com/csharp/readtext/) 在本文中,我们展示了如何在 C# 中读取文本文件。 [C# 教程](http://zetcode.com/lang/csharp/)是有关 C# 语言的综合教程。 C# 中的输入&输出基于流。 `Stream`是所有流的抽象基类。 流是字节序列的抽象,例如文件,输入/输出设备,进程间通信管道或 TCP/IP 套接字。 ## C# 流 `Stream`为输入和输出的类型提供通用接口,并将编程器与操作系统和底层设备的特定详细信息隔离开。 例如,`MemoryStream`处理内存中的数据,`FileStream`处理文件中的数据。 `thermopylae.txt` ```cs The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece. ``` 在我们的示例中,我们将读取以下文件: ## C# 使用`File.ReadAllText`读取文本文件 `File.ReadAllText()`方法打开一个文本文件,将文件的所有行读取为字符串,然后关闭文件。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace ReadAllText { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; string content = File.ReadAllText(path, Encoding.UTF8); Console.WriteLine(content); } } } ``` 该示例读取`thermopylae.txt`文件的内容并将其打印到控制台。 ## C# 使用`File.ReadAllLines`读取文本文件 `File.ReadAllLines()`打开一个文本文件,将文件的所有行读入字符串数组,然后关闭文件。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace ReadAllLines { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; string[] lines = File.ReadAllLines(path, Encoding.UTF8); foreach (string line in lines) { Console.WriteLine(line); } } } } ``` 使用`File.ReadAllLines()`方法读取`thermopylae.txt`文件的内容并将其打印到控制台。 ```cs foreach (string line in lines) { Console.WriteLine(line); } ``` 我们遍历数组并打印其元素。 ## C# 使用`StreamReader`读取文本文件 `StreamReader`设计用于以特定编码输入字符。 它用于从标准文本文件中读取信息行。 ### 使用`StreamReader`的`ReadToEnd` `ReadToEnd()`方法从流的当前位置到其末尾读取所有字符。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace StreamReaderReadToEnd { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string content = sr.ReadToEnd(); Console.WriteLine(content); } } } ``` 该示例使用`StreamReader`的`ReadToEnd()`方法读取文件。 ```cs using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); ``` `FileStream`类为文件提供`Stream`,支持同步和异步读取和写入操作。 构造器使用指定的路径,创建模式和读/写权限初始化`FileStream`类的新实例。 ```cs using var sr = new StreamReader(fs, Encoding.UTF8); ``` `FileStream`被传递到`StreamReader`。 ```cs string content = sr.ReadToEnd(); ``` `StreamReader`的`ReadToEnd()`方法读取从当前位置到文件结尾的所有字符。 ### 使用`StreamReader`的`ReadLine` `StreamReader`的`ReadLine()`方法从当前流中读取一行字符,并将数据作为字符串返回。 `Program.cs` ```cs using System; using System.IO; using System.Text; namespace StreamReaderReadLine { class Program { static void Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string line = String.Empty; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } } ``` 该代码示例逐行读取文件。 ```cs string line = String.Empty; while ((line = streamReader.ReadLine()) != null) { Console.WriteLine(line); } ``` 在`while`循环中,我们使用`StreamReader`的`ReadLine()`方法逐行读取文件的内容。 ## C# 与`StreamReader`的`ReadToEndAsync`异步读取文本文件 `ReadToEndAsync()`方法异步读取从当前位置到流末尾的所有字符,并将它们作为一个字符串返回。 `Program.cs` ```cs using System; using System.IO; using System.Text; using System.Threading.Tasks; namespace ReadTextFileAsync { class Program { static async Task Main(string[] args) { var path = @"C:\Users\Jano\Documents\thermopylae.txt"; using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string content = await sr.ReadToEndAsync(); Console.WriteLine(content); } } } ``` 在下一个示例中,我们异步读取文本文件。 ```cs static async Task Main(string[] args) ``` `async`修饰符允许在`Main()`方法中进行异步操作。 ```cs string content = await sr.ReadToEndAsync(); ``` `await`运算符应用于异步方法中的任务,以暂停该方法的执行,直到等待的任务完成。 在本文中,我们已经以各种方式在 C# 中阅读了文本文件。 您可能也对以下相关教程感兴趣: [MySQL C# 教程](/db/mysqlcsharptutorial/), [C# 中的日期和时间](/articles/csharpdatetime/),[用 C# 阅读网页](/csharp/readwebpage/)或 [C# Winforms 教程](/gui/csharpwinforms/)。