Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a text file and updating

Tags:

c#

io

I am new to programming and I came across a problem and I'm not sure how to deal with it.

I use the line

    textBox2.Text = System.IO.File.ReadAllText(path);

To read from a text file and paste the contents in textBox2.

Now the issue is that the text file I'm try to read is a large (couple megabytes) text file. This text file contains logs from a program, new logs are always added at the bottom of the file.
Now I want to update textBox2 if the text file is updated. However I am not sure how to do this in an efficient way. One way is to just read the whole text file again, but since the text file is so big, this is a very slow process.

I am interested in finding out a different and faster way to handle this. I'm not really interested in the exact code, I just hoped to find out in what direction I should look and what options I can consider.

like image 682
Burak Avatar asked Jan 14 '23 17:01

Burak


1 Answers

Well, two obvious things you could check:

  • The size of the file (FileInfo.Length)
  • The last write time (FileSystemInfo.LastWriteTimeUtc)

If you keep track of those, you should be able to detect when the file has changed - at least with a reasonable degree of confidence.

Additionally, you can use FileSystemWatcher to watch for changes.

Also, you might want to consider keeping track of where you've read to - so you could just read the new data, by seeking to the right place in the file.

Finally, a TextBox may not really be the best user interface for a huge log file. If this is a structured log file, it would be good to have that structure represented in the UI - for example, one row in a table per log entry, potentially with filtering options etc.

like image 59
Jon Skeet Avatar answered Jan 19 '23 01:01

Jon Skeet