日志文件出现问题我需要解析并显示数据网格视图中的某些字段。日志文件是一个大的制表符分隔文件,我可以读取它并轻松解析数组。问题是我想从文件的END开始并向后读,直到我在其中一个字段中达到某个值。然后我想再次开始向前阅读,直到它到达文件的末尾。虽然它正在读到最后,我希望它用某些字段填充数据网格视图。这是我到目前为止所拥有的 -
查看代码 - 它需要从末尾读取,直到第4个字段等于零,然后再次开始向前读取。我也可以从最后读取,将值保存到数据网格视图,直到我达到0,如果这更容易。
感谢您的帮助!
另一种选择是读取文件并反向保存,一行一行 - 最后一行第一行,第二行到最后一行第二行等等。这将是非常好的,因为我有另一个需要的功能阅读文件,它也需要从最后到开头。
Private Sub btnParseLog_Click(sender As Object, e As EventArgs) Handles btnParseLog.Click
Dim FileLocation As String = "BLAHBLAHBLAH"
Dim LogFile As String = "LOGFILE"
DataGridView1.ColumnCount = 3
DataGridView1.ColumnHeadersVisible = True
' Set the column header style.
Dim columnHeaderStyle As New DataGridViewCellStyle()
columnHeaderStyle.BackColor = Color.Beige
columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
DataGridView1.Columns(0).Name = "User"
DataGridView1.Columns(1).Name = "Number"
DataGridView1.Columns(2).Name = "Time"
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(LogFile)
MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {vbTab}
Dim TotalLineCount As Integer = File.ReadAllLines(LogFile).Length
Dim currentRow As String()
Dim RowCount As Integer = 0
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
RowCount = RowCount + 1
' Include code here to handle the row.
If RowCount = TotalLineCount Then
While Not currentRow(4).ToString = 0
DataGridView1.Rows.Add(currentRow(1).ToString, currentRow(4).ToString, currentRow(7).ToString)
RowCount = RowCount - 1
End While
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
" is invalid. Skipping")
End Try
End While
End Using
End Sub