C#如何读取一行文字

来源:百度知道 编辑:UC知道 时间:2024/06/01 14:25:00
最好是这样:
(下面是示例文本)
1aaaa
2bbbb
3cccc

第一次运行方法获得“1aaaa”,第二次运行方法获得“2bbbb”。
我说的是对字符串的操作

using(StreamReader sr=new StreamReader(System.Web.HttpContext.Current.Server.MapPath("default.aspx"),System.Text.Encoding.GetEncoding("gb2312")))
{
string line;
while((line=sr.ReadLine())!=null)
{
//line就是你想读的每行; }
sr.Close();
}

假设文本都在一个字符串str里
string[] strs = str.split('\n');
应该就把每行放在数组里了
fox可以试试

是控制台还是窗体?
控制台就Console.ReadLine();
窗体的话,textBox和richTextBox直接用Lines属性,Lines[0]第一行,Lines[1]第二行。。。

代码都贴给你

FileStream fs = new FileStream("文件路径");
System.IO.StreamReader sr = new StreamReader(fs);
string result = sr.ReadLine();

简单的三行,但是包括很多含义,可以仔细体会一下。

如果是文本文件.
FileStream fs = new FileStream(txtfilename, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
sr.ReadLine();//读取一行;

StreamReader sr = new StreamReader(filepath);
string s