Vb.net 试图从资源中读取带分隔符的文本文件-但它不会运行

Vb.net 试图从资源中读取带分隔符的文本文件-但它不会运行,vb.net,file,Vb.net,File,我遇到了一个问题,我没有从位置字符串中读取文本文件,而是将其更改为从资源位置读取文本文件,从而中断了我的程序。我还使用了insert snippet方法来获取大部分代码,因此可以肯定地说,我不知道发生了什么。能找个人帮忙吗 '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

我遇到了一个问题,我没有从位置字符串中读取文本文件,而是将其更改为从资源位置读取文本文件,从而中断了我的程序。我还使用了insert snippet方法来获取大部分代码,因此可以肯定地说,我不知道发生了什么。能找个人帮忙吗

   '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
错误

例如ToString()
“System.ArgumentException:路径中存在非法字符。 在System.IO.Path.CheckInvalidPathChars处(字符串路径) 在System.IO.Path.NormalizePathFast(字符串路径,布尔fullCheck) 在System.IO.Path.NormalizePath(字符串路径,布尔满校验) 位于System.IO.Path.GetFullPathInternal(字符串路径) 在System.IO.Path.GetFullPath(字符串路径)处 位于Microsoft.VisualBasic.FileIO.FileSystem.NormalizePath(字符串路径) 位于Microsoft.VisualBasic.FileIO.TextFieldParser.ValidatePath(字符串路径) 位于Microsoft.VisualBasic.FileIO.TextFieldParser.InitializeFromPath(字符串路径、编码defaultEncoding、布尔检测编码) 位于Microsoft.VisualBasic.FileIO.TextFieldParser..ctor(字符串路径)
在I:\vb\HangMan\HangMan\Form1.vb:line 274“String”中的HangMan.Form1.GetWords()处调试此代码时,从My.Resources.GamesList读取变量filename后,变量filename的值是多少?它是否是有效字符串,是否指向您的文件?

您使用的TextFieldParser构造函数需要文件名。相反,它获取文件的内容。也就是说,文件内容不是文件的有效路径。您需要找到接受流的构造函数,并使用StringReader类来提供流。例如:

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

这有点浪费内存,但如果文本小于1兆字节左右,则不是问题。如果超过,则不应将其放在资源中。

您可以发布设置了
My.Resources.GamesList
的位置吗?我猜是资源文件夹?我刚刚将文本文件添加到程序资源中。我这样做的唯一原因是为了让我的程序可以从.exe文件运行。如果我的未注释代码造成了混乱,那么很抱歉。但是这两个文件都应该被命名为proggramingList“它弄乱了我的程序”?你是个孩子吗?它以什么方式“打乱了我的程序”?有例外吗?然后发布完整的异常(ex.ToString(),而不是ex.Message)。对不起,语法是这样的,这是您想要的异常信息吗?文件名应该是这样的。一根很长的绳子。如果你需要的价值,我可以张贴它非常感谢你!谢谢你向我解释发生了什么事。就像它应该的那样工作!你就是那个人!我的排名不高,不足以使它有帮助。对不起,我试过了:(谢谢,我一定会回来的,你帮了我很多