Unity(C#)怎么提取一个txt文件特定行数的内容?

怎么提取出txt特定行数的内容
要代码

一行的方法就够了:
private static IEnumerable<string> GetTxtContents (string path, int start = 0, int count = int.MaxValue)
{
return File.ReadAllLines(path.ToString()).ToList().Skip(start).Take(count);
}
参数说明:
path:txt文件路径
start:开始行数,默认从第0行开始(即首行)
count:读入行数,默认全部读入
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-21
只有手机,帮你手打先试试,不行追问我再帮改
Public string ReadLineAt(int lineNumber, string path)
{
FileInfo fi=new FileInfo(path);
if(!fi.exist) return "文件不存在";
FileStream fs=new FileStream(path, FileMode.Open);
StreamReader sr=new StreamReader(fs);
for(int i=0;i<lineNumber-1)
{
if(fs.EndOfStream)
return "超出文本区域";
sr.ReadLine();
}
if(fs.EndOfStream)
return "超出文本区域";
return sr.ReadLine();
}追问

不用,我自己已经学会了 ReadAllLine赋值给一个string[]就好了

追答

也可以的,主要看你是否需要考虑占用内存空间的大小,如果其他也有用,最好是都读进来,如果其他没用,可以再考虑~当然,配置文件小就无所谓了
祝开发顺利

本回答被提问者和网友采纳
相似回答