gpt4 book ai didi

c# - FileSystemWatcher 和最后一行文本文件

转载 作者:太空宇宙 更新时间:2023-11-03 20:04:10 26 4
gpt4 key购买 nike

我正在尝试在我的应用程序的 Windows 服务中添加 FileSystemWatcher。在我的例子中,我有一个不断更新的文本文件,可能是一行或多行,每次文件得到更新我需要阅读所有那些以前没有读过的文本文件行。

我用谷歌搜索了一下才知道

File.ReadText(@"C:\Filename.txt").Last();

文件更新后会给我文本文件的最后一行,但我不确定它是会给出所有未读行还是只给出文本文件的最后一行。而且我的文本文件正在逐行更新。

无论哪种情况,可能的解决方案是什么。

如果文本文件逐行更新,FileSystemWatcher 也会看到多次将最后一行添加到文件中。

请帮帮我。

更新代码..

using (var sr = new StreamReader(@"C:\Temp\LineTest.txt")) 
{
string line;
long pos = 0;
while ((line = sr.ReadLine()) != null)
{
Console.Write("{0:d3} ", pos);
Console.WriteLine(line);
pos += line.Length;
}
}

更新代码。

 public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

private System.Threading.Thread _thread;
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
int lineCount;
long previousLength = 0;
string filepath = "C:\\Temp\\LineTest.txt";


public void OnDebug()
{

OnStart(null);

}

protected override void OnStart(string[] args)
{

_thread = new Thread(addLogic);
_thread.Start();


}

//This event is raised when a file is changed
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
_thread = new Thread(addlogic);
_thread.Start();
}


public static string[] ReadFromFile(string filePath, int count, ref int lineCount)
{
lineCount += count;
return File.ReadLines(filePath).Skip(lineCount).Take(count).ToArray();
}
public void addlogic()
{
//Add Logic Here
//How to use lineCount here to read specific line that i am not getting

//If all textfile gets traversed then is FileSystemWatcher
FileSystemWatcher Watcher = new FileSystemWatcher(filepath);
Watcher.EnableRaisingEvents = true;
Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);

}
}



protected override void OnStop()
{
_shutdownEvent.Set();
_thread.Join(); // wait for thread to stop
}
}
}

最佳答案

我只是写了一个简单的类来读取添加的行。这实际上读取了附加到文件的任何内容,即使在同一行也是如此。

public class AddedContentReader
{

private readonly FileStream _fileStream;
private readonly StreamReader _reader;

//Start position is from where to start reading first time. consequent read are managed by the Stream reader
public AddedContentReader(string fileName, long startPosition = 0)
{
//Open the file as FileStream
_fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_reader = new StreamReader(_fileStream);
//Set the starting position
_fileStream.Position = startPosition;
}


//Get the current offset. You can save this when the application exits and on next reload
//set startPosition to value returned by this method to start reading from that location
public long CurrentOffset
{
get { return _fileStream.Position; }
}

//Returns the lines added after this function was last called
public string GetAddedLines()
{
return _reader.ReadToEnd();
}


}

你可以这样调用它。

private AddedContentReader _freader;

protected override void OnStart(string[] args)
{
_freader = new AddedContentReader("E:\\tmp\\test.txt");
//If you have saved the last position when the application did exit then you can use that value here to start from that location like the following
//_freader = new AddedContentReader("E:\\tmp\\test.txt",lastReadPosition);

}
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
string addedContent= _freader.GetAddedLines();
//you can do whatever you want with the lines
}

注意:我没有用非常快的更新测试这个。

关于c# - FileSystemWatcher 和最后一行文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com