[英]Read only the first few lines of text from a file
How can I read just the first two lines of a file my program saves? 我怎样才能读出程序保存的文件的前两行? (They represent a username and a password.) (它们代表用户名和密码。)
Use a System.IO.StreamReader
. 使用System.IO.StreamReader
。
string line1, line2;
using (StreamReader reader = new StreamReader("myFile.txt")) {
line1 = reader.ReadLine();
line2 = reader.ReadLine();
}
Or, for something modern: 或者,对于现代的东西:
var lines = File.ReadLines("myFile.txt").Take(2).ToArray();
为此使用StreamReader.ReadLine()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.