只读了前几个文本从一个文件中的行 [英] Read only the first few lines of text from a file

查看:114
本文介绍了只读了前几个文本从一个文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何读取只是一个文件我的程序保存的头两行? (他们代表一个用户名和密码。)

How can I read just the first two lines of a file my program saves? (They represent a username and a password.)

推荐答案

使用的 就是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 = ReadLines("myFile.txt").Take(2).ToArray();

这篇关于只读了前几个文本从一个文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆