从文本文件填充数组

时间:2015-01-16 03:19:07

标签: arrays vb.net

所以我试图从文本文件填充数组。我在Visual Basic中(我在一年之内没有碰到过,并且从高中课程中获得的知识非常有限。)我正在读取文本,我试图将其从各种其他资源中放入一个阵列中,除了没有真正读取数组。文本文件中的最后一个值是存储的值,我不确定如何修复它。这是我到目前为止的代码:

Dim sr As New StreamReader("text file location")       
Dim words(292) As String
Dim text as String = ""
Dim i As Integer = 0
    Do Until sr.Peek = -1
        text = sr.ReadLine()
        words(i) = text
        lstWords.Items.Add(words(i))
    Loop

我是StackOverFlow社区的新手,并希望得到任何能够提供帮助的人的帮助!提前谢谢!

1 个答案:

答案 0 :(得分:1)

你正在艰难地做这件事。试试这个:

Dim words() As String = File.ReadAllLines("text file location") 

因为你正在加载一个列表框:

lstWords.Items.AddRange(File.ReadAllLines("text file location"))