本文介绍了使用streamReader读取文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读取放在目录中某个位置的文件,如果我得到类似连接重置或异常的内容,我将整行保存在接下来的3行。
I''m reading a file placed in somewhere in the directory, If I get something like "Connection reset" or "Exception" I''m storing that entire line with next 3 lines.
Function SearchException(ByVal filePath As String, ByVal fileName As String)
Dim strTextFile As StreamReader
Dim rowLength As Integer = 0
Dim eachLine As String
Dim strFilePath = filePath & "\" & fileName
Dim flagChk As Boolean
strTextFile = File.OpenText(filePath & "\" & fileName)
txtVewLogDetail.Text = strTextFile.ReadToEnd
''TextBox1.Text = fileName.Remove(10)
Dim strStartupLine As String
strStartupLine = getStartupDate(fileName.Remove(10))
Dim r As StreamReader = New StreamReader(strFilePath)
Dim aryShowString() As String
Dim intLine As Integer = 0
'rowLength = File.ReadAllLines(strFilePath).Length
While (r.ReadLine() IsNot Nothing)
rowLength = rowLength + 1
eachLine = r.ReadLine()
If (eachLine.Contains("Connection reset")) Then
aryShowString(0) = eachLine.ToString()
intLine = intLine + 1
MsgBox("Exception Found at Row = " & rowLength)
'flagChk = True
End If
End While
END SUB
这里aryShowString是一个数组,当我试图存储每个字符串本身的内容时,它不起作用。
请帮帮我...
Here aryShowString is an array, when I''m trying to store the content of eachLine which is a string itself, is not working.
Please Help me on this...
推荐答案
Dim r As StreamReader = New StreamReader(strFilePath)
Dim lines As New List(Of String)()
Dim intLine As Integer = 0
'rowLength = File.ReadAllLines(strFilePath).Length
While (r.ReadLine() IsNot Nothing)
rowLength = rowLength + 1
eachLine = r.ReadLine()
If (eachLine.Contains("Connection reset")) Then
lines.Add(eachLine)
intLine = intLine + 1
MsgBox("Exception Found at Row = " & rowLength)
'flagChk = True
End If
End While
如果你真的需要列表最后作为数组,你可以使用ToArray方法。
If you really need the list as an array at the end, you can use the ToArray method.
这篇关于使用streamReader读取文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!