내 응용 프로그램의 Windows 서비스에 FileSystemWatcher
을 추가하려고합니다. 제 경우에는 연속적으로 업데이트되는 텍스트 파일이 한 줄 또는 두 줄 이상일 수 있으며 파일이 가져올 때마다 업데이트 나는 이전에 읽지 않은 모든 텍스트 파일 행을 읽을 필요가있다. FileSystemWatcher 및 텍스트 파일의 마지막 줄
File.ReadText(@"C:\Filename.txt").Last();
나에게 TEXTFILE의 마지막 줄을 줄 것이라는 점을 알고있어하지만 난이 모든 읽지 않은 행 또는 마지막 만 제공할지 확실하지 않다 또한 textfile.Also 내 텍스트 파일을 줄 단위로 업데이트되고 있습니다.
두 경우 모두 가능한 해결책은 무엇입니까?
텍스트 파일이 한 줄씩 업데이트되는 경우 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
}
}
}
여러 행을 추가하는 경우 서비스에서 읽은 행을 추적해야합니다. 나는 너 자신을 항상 읽는 선을 추적하는 것이 좋습니다. – bansi
@bansi Line은 텍스트 파일에 한 번에 하나씩 만 추가되고 있으며 또한 읽음 및 읽지 않은 행을 분리 할 수있는 행에 고유 한 부분이 없습니다. 또한 읽은 행 추적에 관해서는 어떻게 진행할 수 있는지 말해주십시오 . – user3816352
마지막으로 읽은 오프셋을 저장하고 다음부터 그 위치에서 시작할 수 있습니다. – bansi