TextFieldParser中的ReadFields()抛出异常

时间:2017-12-15 06:52:03

标签: arrays string vb.net csv textfieldparser

我在一个文件夹中有一堆csv文件。这是一个示例:

Item    Value
Row1    Val1
Row2    Val2
Row3    Val3
Row4    Val4"
Row5    Val5

我根据该文件夹中所有csv文件中的可用信息编写了一个代码来绘制图表。这是我的按钮点击事件:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles generatePlot.Click

        Dim dirs As FileInfo() = GetFilesInDirectory("*.csv", True) 'Get all the csv file from result folder in descending order (creation date)
        Dim diNext As FileInfo

        Try
            For Each diNext In dirs.Reverse
                Using MyReader As New FileIO.TextFieldParser(diNext.FullName)
                    MyReader.TextFieldType = FileIO.FieldType.Delimited
                    MyReader.SetDelimiters(",")
                    Dim currentRow As String()
                    While Not MyReader.EndOfData
                        currentRow = MyReader.ReadFields()
                        processRow(diNext, currentRow)
                    End While
                End Using
            Next
        Catch ex As Exception
            MessageBox.Show(ErrorToString)
        End Try

        'Save chart as an image
        Chart1.SaveImage(imageSave, System.Drawing.Imaging.ImageFormat.Bmp)

    End Sub

如果查看我的示例csv, Row4的值为Val4" 。注意它中的双引号。而且,我的代码中的 currentRow = MyReader.ReadFields()出现异常,其中第5行无法使用当前分隔符进行分析。我知道原因是因为存在双引号。由于这是一个字符串数组,我认为我需要创建一个函数来处理数组中的每个项目并删除双引号。但是,即使在我处理字符串数组之前抛出异常,我也无法做到。

关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:1)

StreamReader can be used阅读文本文件,只需查看下面的示例即可满足您的需求:

请注意,您不需要MemoryStreamWriter,只需Reader

Public Sub ReadTest()
    Using MemoryStream As New IO.MemoryStream()
        Dim Writer As New IO.StreamWriter(MemoryStream) 'Writing on a memory stream to emulate a File
        Writer.WriteLine("Item,Value")
        Writer.WriteLine("Row1,Val1")
        Writer.WriteLine("Row2,Val2")
        Writer.WriteLine("Row3,Val3")
        Writer.WriteLine("Row4,Val4""")
        Writer.WriteLine("Row5,Val5")

        Writer.Flush()
        MemoryStream.Position = 0 'Reseting the MemoryStream to Begin Reading

        Dim Reader As New IO.StreamReader(MemoryStream) 'Reading from the Memory but can be changed into the File Path
        While Not Reader.EndOfStream
            Dim Line As String = Reader.ReadLine
            Dim Values() = Line.Split(",")
            'Values(0) will contain the First Item
            'Values(1) will contain the second Item

            Values(1).Replace("""", "") 'Remove the quote from the value string

        End While

    End Using
End Sub

答案 1 :(得分:0)

感谢@jmcilhinney和@AugustoQ提出的建议。

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles generatePlot.Click

        Dim dirs As FileInfo() = GetFilesInDirectory("*.csv", True) 'Get all the csv file from result folder in descending order (creation date)
        Dim diNext As FileInfo
        Dim currentRow As String()
        Try
            For Each diNext In dirs.Reverse
                For Each rawRows As String In File.ReadLines(diNext.FullName)
                    currentRow = processRawRow(rawRows)
                    processRow(diNext, currentRow)
                Next
            Next
        Catch ex As Exception
            MessageBox.Show(ErrorToString)
        End Try

        'Save chart as an image
        Chart1.SaveImage(imageSave, System.Drawing.Imaging.ImageFormat.Bmp)

    End Sub

我完全替换了TextFieldParser并使用了这个函数:

Private Function processRawRow(ByVal rawRows As String) As String()

        rawRows = rawRows.Replace("""", "").Trim()
        Dim processedList = rawRows.Split(",")
        Return processedList

    End Function

它完美无缺。谢谢大家...