Reading from a text file and updating

I am new to programming and I ran into a problem and I am not sure how to deal with it.

I am using string

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

To read from a text file and paste content into textBox2.

Now the problem is that the text file I'm trying to read is a large (a couple of megabytes) text file. This text file contains the logs from the program, new entries 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 simply read the entire text file again, but since the text file is so large, it is a very slow process.

I am interested in learning a different and faster way to handle this. I am not interested in the exact code, I was just hoping to find out in which direction I should look and which options I can consider.

+4
source share
2 answers

Ok, two obvious things you could check:

If you track them, you will be able to detect when the file has changed - at least with a reasonable degree of certainty.

Alternatively, you can use FileSystemWatcher to view changes.

In addition, you may need to keep track of where you read, so you can just read the new data, looking for the right place in the file.

Finally, a TextBox may not be the best user interface for a huge log file. If it is a structured log file, it would be nice to have this structure presented in the user interface - for example, one row in the table for recording in the log, potentially with filtering options, etc.

+8
source

You can check every X seconds. If the file has changed, update, if not, do nothing. You can save the file modification time to see if it has changed or not.

+1
source

All Articles