C#中如何读取文本文件指定行数的数据

C#中如何读取文本文件指定行数的数据,用哪个类的什么方法。

第1个回答  2010-09-01
用StreamReader的ReadLine就可以了
但要一行行地读直道指定那行

下面有个例子,参考下吧

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}本回答被提问者采纳
相似回答