如何:一次一行地读取文本文件 (Visual C#)

本示例使用 StreamReader 类的 ReadLine 方法将文本文件的内容读取(一次读取一行)到字符串中。所有文本行都保存在字符串 line 中并显示在屏幕上。

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
    new System.IO.StreamReader(@"c:\test.txt");
while((line = file.ReadLine()) != null)
{
    System.Console.WriteLine (line);
    counter++;
}

file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();

编译代码

复制该代码,并将其粘贴到某控制台应用程序的 Main 方法中。

将 "c:\test.txt" 替换为实际的文件名。

可靠编程

以下情况可能会导致异常:

  • 该文件可能不存在。

.NET Framework 安全性

不要根据文件的名称来判断文件的内容。例如,文件 myFile.cs 可能不是 C# 源文件。

请参阅

System.IO

C# 编程指南

文件系统和注册表(C# 编程指南)