Need to read the contents of a text file while it is open in another application for writing...

  • Thread starter Thread starter coxnews
  • Start date Start date
C

coxnews

Hiya,

I need to open and read a text based log file in real time as it is being
written to by another application. Am using VB.NET in a windows forms
application.

I have attempted to use a streamreader, but get a error.

Any kick in the right direction is most appreciated!

Dave Borneman
 
Which error do you get?

In any event, if the application that is writing to the file has denied
read access to other processes, then you are out of luck. If the other
application has not opened the file exclusively, then you can use a
StreamReader. Just be sure to specify the correct sharing mode when
you open the file.
 
As you said in real time:

If the file is being written to it will be locked for write access, so, you
will get the file is being used by another process error when trying to read
it.

You will need to have the file written to using stream writer, close it. Use
thread.sleep for a split second & then open for read access. After which
time, close it, get the thread to sleep again before opening it again.

Dim sw as New IO.StreamWriter("Your File Here With Extension")
sw.WriteLine("Your Data Here")
sw.Flush()
sw.Close
System.Threading.Thread.Sleep(200) ' 0.2 of a sec
Dim sr As New StreamReader("Your File Here With Extension")
Dim strLine As String
strLine = sr.ReadLine()
' Process your data here
sr.Close()
System.Threading.Thread.Sleep(200) ' 0.2 of a sec

Do the above in a loop & use a variable for the filename & path.

I hope this helps
 
Back
Top