intrixius
intrixius

Reputation: 1136

how to read a CSV file as resource using TextFieldParser

I have a CSV file in my project resources which I want to read using FileIO.TextFieldParser

I tried Dim parser = new TextFieldParser(My.Resources.ArticlesCSV), but since TextFieldParser expects either a path (as string) or a stream, this is not working. I guess one possibility is to convert the resource to a stream, but I cannot find how to do that...

What is the best way to get this working?

Upvotes: 0

Views: 1196

Answers (1)

A Friend
A Friend

Reputation: 2750

You can create a new instance of IO.StringReader which is of type TextReader that TextFieldParser will accept. Just pass your CSV file (Thanks to AndrewMorton)

Using strReader As New IO.StringReader(My.Resources.ArticlesCSV)
    Using textparser As New TextFieldParser(strReader)
        textparser.Delimiters = {","}
        While Not textparser.EndOfData
            Dim curRow = textparser.ReadFields()
            ' Do stuff
        End While
    End Using
End Using

Upvotes: 3

Related Questions