tags:

views:

96

answers:

2

I'm having a problem where instead of reading a text file from the location string, I changed it to read the text file from the resource location and it breaks my program. I've also used the insert snippet method to get most of this code, so it is safe to say I don't know what is going on. Could some one 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

the error

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) at Microsoft.VisualBasic.FileIO.TextFieldParser.InitializeFromPath(String path, Encoding defaultEncoding, Boolean detectEncoding) at Microsoft.VisualBasic.FileIO.TextFieldParser..ctor(String path) at HangMan.Form1.GetWords() in I:\vb\HangMan\HangMan\Form1.vb:line 274" String

A: 

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

Steve Sheldon
file name is what it should be. A very long string. If you need the value i can post it
Bigfatty
A: 

The TextFieldParser constructor you use expects the name of a file. Instead, it gets the contents of the file. That goes Kaboom, the file content is not a valid path to a file. You'll need to the constructor that takes a Stream and use the StringReader class to provide the 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

This is a bit wasteful of memory but not an issue if the text is less than a megabyte or so. If it is more then you shouldn't put it in a resource.

Hans Passant
Thank you very much! thanks for explaining to me what was going on too. works just like it should! you are the man!
Bigfatty
@Big: oh man, good answer but not helpful? Double-you, tee-eff?
Hans Passant
i'm not ranked high enough to make it helpful. sorry, i did try :(
Bigfatty
@Big: okay, you're right, sorry. Well, come back some day when you're swimming in "good question" upvotes. Gave you one to get there, it *was* a good question.
Hans Passant
thanks, i'll make sure i come back to give it, you've helped me so much
Bigfatty