Arrays VB2010数组来自TXT文件

Arrays VB2010数组来自TXT文件,arrays,vb.net,string,Arrays,Vb.net,String,我需要创建一个程序来打开文本文档,并为从txt到一个字符串的任何行添加新数组。我尝试使用此代码,但不起作用: Dim x As New List(Of String) Dim o As New IO.StreamReader("c:\*.txt") For Each a As String In o.ReadToEnd() x.Add(a) Next 有人能帮我吗?谢谢。也许这对你有用 Dim r As New StreamReader(filename) Dim x As

我需要创建一个程序来打开文本文档,并为从txt到一个字符串的任何行添加新数组。我尝试使用此代码,但不起作用:

Dim x As New List(Of String)
Dim o As New IO.StreamReader("c:\*.txt")
For Each a As String In o.ReadToEnd()
     x.Add(a)
Next

有人能帮我吗?谢谢。

也许这对你有用

  Dim r As New StreamReader(filename)
  Dim x As New List(Of String)
  TextBox1.Text = ""
  Do While r.Peek > -1
     x.add r.ReadLine
  Loop
  r.Close()
    Dim o As New IO.StreamReader("C:\PathTo\File.txt")
    Dim x() As String = o.ReadToEnd.Split(CChar(Convert.ToChar(10) & Convert.ToChar(13)))

    For Each elementX As String In x
        Debug.WriteLine(elementX) ' display text from the file
    Next
ReadToEnd将整个文件读入一个字符串,因此您将无法在这些行上循环

让你的生活更轻松,写作更轻松

Dim lines() As String = File.ReadAllLines("c:\myfile.txt")
这将使用一行代码将所有行读入字符串数组

注意:文件名中的通配符无效

如果仍要使用StreamReader,请按以下方式操作:

Dim lines = New List(Of String)()
Using reader = New StreamReader("C:\MyFile.txt")
    While Not reader.EndOfStream
        lines.Add(reader.ReadLine())
    End While
End Using

Using命令负责自动关闭读卡器。

是否有任何错误?在For Each循环之后,x的内容是什么?我们需要更多信息。字符串=File.ReadAllLinespath这样的暗线有什么问题?但“不工作”是对您希望得到帮助的内容的一个非常糟糕的描述