如何将txt文件中的内容用循环一行一行读出来啊?
StreamReader sr;
string strContent;
sr=File.OpenText(@"D:\CS\Test.txt");
//读第一行
strContent = sr.ReadLine();
sr.Close();
Console.WriteLine(strContent);
//循环读所有的内容
while((strContent = sr.ReadLine()) != null) {
Console.WriteLine(strContent);
}
sr.Close();
支持中文:
StreamReader str=new StreamReader(@"D:\CS\Test.txt",Encoding.GetEncoding("gb2312"));
string strContent;
sr=File.OpenText(@"D:\CS\Test.txt");
//读第一行
strContent = sr.ReadLine();
sr.Close();
Console.WriteLine(strContent);
//循环读所有的内容
while((strContent = sr.ReadLine()) != null) {
Console.WriteLine(strContent);
}
sr.Close();
支持中文:
StreamReader str=new StreamReader(@"D:\CS\Test.txt",Encoding.GetEncoding("gb2312"));