Log file scan problem

romez

New member
Joined
Apr 11, 2008
Messages
1
Programming Experience
1-3
I have a log file (eg logfile.txt) which is continously updated (with the values on and off from various equipments) such as:


VB.NET:
CCC:on
DDD:on
XXX:off

CCC:off
EEE:on
FFF:on

DDD:off
EEE:off
XXX:on
Now i have buttons on a vb.net form corresponding to each of the equipments such as CCC, DDD, EEE, XXX and FFF. I have to have a program which continuosly reads the updated value in the log file and change the color of the button to green (on) and blue (off). The log file is updated every 1 second so new values for all the equipments have to be scanned (the values are appended in line wise as shown above). Can anyone please help to guide as to how can i read and scan the file for the updated values only, every SECOND so that i can have the corresponding buttons turned on green and red depending on the values read.

Thanks .... PLEASE HELP

Romez
 
If I understand, you want to only read the last section added, after the double linefeed. In any case you have to read all the text in the file. Then find that double linefeed. Then split the remaining text up in lines. Then process each line, splitting by the colon. Then find the control matching the first part. Then set color depending on the last part. All this is can be done with simple string parsing, using basic String members like SubString, LastIndexOf and Split. Example:
VB.NET:
Dim text As String = My.Computer.FileSystem.ReadAllText("log.txt")
text = text.Substring(text.LastIndexOf(vbNewLine & vbNewLine))
Dim lines() As String = text.Split(New String() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
For Each line As String In lines
    Dim data() As String = line.Split(":")
    Dim c As Control = Me.Controls(data(0))
    If c IsNot Nothing Then
        c.BackColor = IIf(data(1) = "on", Color.Green, Color.Blue)
    End If
Next
To do this periodically use a Timer.
 
Back
Top