From: Udi on 11 May 2010 03:22 Thanks all, I appreciate the help. The file is a given fact. It is created and written by a third party application. So unless there's a way to treat the file as a named pipe, i guess i'm stuck with saving the position between the calls? Udi
From: Udi on 11 May 2010 03:25 BTW, waiting for the writing process to end is not an option since i need to start parsig the file as soon as it is created. (it is kind of a log file) Thanks again, Udi
From: Peter Duniho on 11 May 2010 03:39 Udi wrote: > Thanks all, I appreciate the help. > > The file is a given fact. It is created and written by a third party > application. > So unless there's a way to treat the file as a named pipe, > i guess i'm stuck with saving the position between the calls? As Arne says, you could look at the "modified" time stamp for the file. Also, the FileSystemWatcher class may help you monitor changes to the file, to know when to try to read more data from it. Pete
From: Arne Vajhøj on 12 May 2010 22:00 On 11-05-2010 03:22, Udi wrote: > The file is a given fact. It is created and written by a third party > application. > So unless there's a way to treat the file as a named pipe, > i guess i'm stuck with saving the position between the calls? I think so. See below for some code to get you started. Arne =========================== using System; using System.IO; using System.Threading; namespace E { public class TailSniff { private string filename; private long pos; public TailSniff(string filename) { this.filename = filename; pos = 0; } public StreamReader Read() { FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Write); long len = fs.Length; fs.Seek(pos, SeekOrigin.Begin); byte[] b = new byte[len - pos]; fs.Read(b, 0, b.Length); fs.Close(); pos = len; return new StreamReader(new MemoryStream(b)); } } public class MainClass { public static void Main(string[] args) { TailSniff ts = new TailSniff(args[0]); for(;;) { StreamReader sr = ts.Read(); string line; while((line = sr.ReadLine()) != null) { Console.WriteLine(line); } sr.Close(); Thread.Sleep(100); } } } }
From: Arne Vajhøj on 12 May 2010 22:00
On 11-05-2010 03:39, Peter Duniho wrote: > Udi wrote: >> The file is a given fact. It is created and written by a third party >> application. >> So unless there's a way to treat the file as a named pipe, >> i guess i'm stuck with saving the position between the calls? > > As Arne says, you could look at the "modified" time stamp for the file. It was the length not the timestamp I suggested looking at. Arne |