Search code examples
c#file-iofileparsingoutput-redirect

Read file while the file is constantly changing c#


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.


Solution

  • Have you tried this stream-reading solution?

         var reader = new System.IO.FileStream("Path", 
                                                     FileMode.Open, 
                                                     FileAccess.Read, 
                                                     FileShare.ReadWrite);