C#怎么从文本文档里读取一行数据?

求一个简单的代码段…谢谢
2024-11-29 18:28:19
推荐回答(3个)
回答1:

string[]strSplit={"**"};

ListstrList=newList();

StreamReadersr=newStreamReader(strPath,Encoding.Default);

while(!sr.EndOfStream)

{

line=sr.ReadLine();

string[]strArr=line.Split(strSplit,StringSplitOptions.RemoveEmptyEntries);

if(textbox1==strArr[0]&&textbox2==strArr[1])

{

line="";

//

//这里对strArr进行删除修改

//

for(inti=0;i

{

line+=strArr[i];

if(i!=strArr.Length-1)

line+="**";

}

}

strList.Add(line);

}

sr.Close();

StreamWritersw=newStreamWriter(strPath,false);

for(inti=0;i

{

sw.WriteLine(strList[i]);

}

sw.Close();

扩展资料

c#删除记事本某一行

stringfileName="E:/aa.txt";

intn=-1;

StringBuildersb=newStringBuilder();

StreamReadersr=newStreamReader(fileName,Encoding.ASCII);

intnLine=0;

while(sr.Peek()>-1)

{

nLine++;

Strings=sr.ReadLine();

if(nLine!=n&&nLine!=3)

sb.Append(s+"/r/n");

}

sr.Close();

StreamWritersw=newStreamWriter(fileName,false,Encoding.ASCII);

sw.Write(sb.ToString());

sw.Close();

回答2:

System.IO.StreamReader s = File.OpenText("test.txt"); //要打开的文本文件,如果跟程序不在一个目录,要包括路径
string read = s.ReadLine();//读取一行,存放在变量read中

回答3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{ StreamReader read = new StreamReader(@"文本路径");
Console.Write(read.ReadLine());
}
}
}