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

怎么提取出txt特定行数的内容要代码
2025-03-12 03:10:48
推荐回答(2个)
回答1:

一行的方法就够了:
private static IEnumerable 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:读入行数,默认全部读入

回答2:

只有手机,帮你手打先试试,不行追问我再帮改
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 {
if(fs.EndOfStream)
return "超出文本区域";
sr.ReadLine();
}
if(fs.EndOfStream)
return "超出文本区域";
return sr.ReadLine();
}