Trying to read a delimited text file from resources - but it won't run

I got a problem where, instead of reading a text file from a location string, I changed it to read a text file from a resource location and it crashes my program. I also used the snippet insert method to get most of this code, so it's safe to say that I don't know what's going on. Can anyone please help?

   'reads the text out of a delimited text file and puts the words and hints into to separate arrays
   ' this works and made the program run
   ' Dim filename As String = Application.StartupPath + "\ProggramingList.txt"
    'this dosnt work and brings back a Illegal characters in path error.
    dim filename as string = My.Resources.ProggramingList
    Dim fields As String()
    'my text files are delimited
    Dim delimiter As String = ","
    Using parser As New TextFieldParser(filename)
        parser.SetDelimiters(delimiter)
        While Not parser.EndOfData
            ' Read in the fields for the current line
            fields = parser.ReadFields()
            ' Add code here to use data in fields variable.

            'put the result into two arrays (the fields are the arrays im talking about). one holds the words, and one holds the corresponding hint
            Programingwords(counter) = Strings.UCase(fields(0))
            counter += 1
            'this is where the hint is at
            Programingwords(counter) = (fields(1))
            counter += 1
        End While
    End Using

      

mistake

ex.ToString ()
"System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars (String path) at System.IO.Path.NormalizePathFast (String path, Boolean fullCheck) at System.IO.Path.NormalizePath (String path, Boolean fullCheck) at System.IO.Path.GetFullPathInternal (String path) at System.IO.Path.GetFullPath (String path) at Microsoft.VisualBasic.FileIO.FileSystem.NormalizePath (String Path) at Microsoft.VisualBasic. FileIO.TextFieldParser.ValidatePath (String path) to Microsoft.VisualBasic.FileIO.TextFieldParser.InitializeFromPath (String path, Encoding defaultEncoding, Boolean detectEncoding) to Microsoft.VisualBasic.FileIO.TextMield.Parser to String () in I: \ vb \ HangMan \ HangMan \ Form1.vb: line 274 "Line

+2


source to share


2 answers


The constructor used, TextFieldParser, expects a file name. Instead, it gets the contents of the file. That is, Kaboom, the content of the file is not a valid file path. You will need a constructor that takes a stream and uses the StringReader class to provide a stream. For example:

Dim fields As String()
Dim delimiter As String = ","
Dim fileContent As String = My.Resources.ProggramingList
Dim stringStream as New System.IO.StringReader(fileContent)
Using parser As New TextFieldParser(stringStream)
  REM etc...
End Using

      



It's a bit wasteful, but not a problem if the text is less than a megabyte or so. If it's more, you shouldn't be putting it into a resource.

+2


source


When you debug this code, what is the meaning of the variable filename after reading it from My.Resources.GamesList? Is this a valid string, does it point to your file?



0


source







All Articles