[英]C# remember current file stream location and jump to the location
I want to remember where, that my program had processed.我想记住我的程序在哪里处理过。 I call StreamReader ReadLine()
我调用StreamReader ReadLine()
Then next time I can jump right back to the location keep processing.然后下次我可以直接跳回到继续处理的位置。
I don't want to do the line count and skip, I want to store a physicial location or something like that.我不想做行数和跳过,我想存储一个物理位置或类似的东西。 So next time I can jump right back.所以下次我可以直接跳回来。
Thanks谢谢
you can count your text length, and then seek it before reading like below code: hope it can help you.你可以计算你的文本长度,然后在阅读之前搜索它,如下代码:希望它可以帮助你。
internal class Program
{
static void Main(string[] args)
{
var fileName = "your file path";
long count = 0;
var firstLine = GetContent(fileName, count);
count += firstLine.position;
var secondLine = GetContent(fileName, count);
count += secondLine.position;
var thirdLine = GetContent(fileName, count);
}
public static (string content, int position) GetContent(string fileName, long position)
{
using var x = File.OpenRead(fileName);
using var ww = new StreamReader(x);
x.Seek(position, SeekOrigin.Begin);
var k = ww.ReadLine();
return (k, ww.CurrentEncoding.GetByteCount(k) + 2);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.