0

I've written a c# application which is supposed to open a selected file, and read all the lines in it.

The goal is to launch application A and redirect its output to file B, and then, using this new app, read the content of the file B and print it to a ListBox.

The thing is that application A is running 24/7 and writes new outputs to the file B, I would like to catch those new lines in real time, and print them to the ListBox.

Is that even possible? I tried using File.ReadAllLines(filename), but that didn't work saying that the file is in use.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Idanis
  • 1,918
  • 6
  • 38
  • 69
  • Possible duplicate of http://stackoverflow.com/questions/9759697/reading-a-file-used-by-another-process – DatRid Apr 08 '14 at 11:04
  • http://stackoverflow.com/questions/897796/how-do-i-open-an-already-opened-file-with-a-net-streamreader see the first answer – Poomrokc The 3years Apr 08 '14 at 11:04
  • You could use the FileSystemWatcher to watch a directory for changes. – scheien Apr 08 '14 at 11:05
  • Also a possible duplicate of http://stackoverflow.com/questions/19229699/read-changes-on-a-text-file-dynamically-c-sharp – Mihai8 Apr 08 '14 at 11:05
  • I don't think it's duplicate , this questions need more than that , he may need what to do after he knows the answer to the questions you guys provide as duplicate – Poomrokc The 3years Apr 08 '14 at 11:08

2 Answers2

0

try this: http://msdn.microsoft.com/en-us/library/aa988006(v=vs.80).aspx
This could resolve the problem of the "File is open".

Nick Prozee
  • 2,823
  • 4
  • 22
  • 49
0

Have you tried this stream-reading solution?

     var reader = new System.IO.FileStream("Path", 
                                                 FileMode.Open, 
                                                 FileAccess.Read, 
                                                 FileShare.ReadWrite);
Ali Dehghan
  • 462
  • 3
  • 6