Vb.net 数组列表中只有一个文件时打印三次

Vb.net 数组列表中只有一个文件时打印三次,vb.net,winforms,Vb.net,Winforms,下面的函数加载ssh目录的文件,并用用户选择下载的文件填充列表视图,但由于某种原因,它填充列表三次,数组列表被分成三个部分,其中(2)部分是文件名,为什么不在这里向列表视图添加一个元素呢 Private Sub LoadSSHFiles() Dim sshController As New wcSFtp(cfb.HostName, cfb.port, cfb.username, cfb.passsword) Dim thisDocument As ArrayList

下面的函数加载ssh目录的文件,并用用户选择下载的文件填充列表视图,但由于某种原因,它填充列表三次,数组列表被分成三个部分,其中(2)部分是文件名,为什么不在这里向列表视图添加一个元素呢

 Private Sub LoadSSHFiles()      
    Dim sshController As New wcSFtp(cfb.HostName, cfb.port, cfb.username, cfb.passsword)
    Dim thisDocument As ArrayList = sshController.getDirectoryList("/")

    Dim cnt As Int16 = 0

    For Each item In thisDocument(2)

        Dim lvItem As New ListViewItem(cnt)
        lvItem.SubItems.Add(thisDocument(2).ToString())
        lvAvailableDocuments.Items.Add(lvItem)
        cnt += 1

    Next
请参见上面添加的注释。通常,当你迭代某件事情时,你会使用索引var做一些事情;你在迭代一件事,然后一遍又一遍地添加那件事。使用ArrayList,您可以更轻松地使用计数:

Dim lvi As ListViewitem

' go thru the list
For n As Integer = 0 to thisDocument.Count - 1
     lvi = new ListViewItem
     lvi.Text = thisDocument(n)
     ' or, since there are no subitems:
     ' lvi = New ListViewItem(thisDocument(n))

     lvAvailableDocuments.Items.Add(lvi)
Next n

现在,您所要做的就是正确地查看
此文档
是否正确地添加了所需文件的种子。

可能
加载文件
会被调用三次。设置一个断点,看看这是什么happens@Plutonix否仅在表单加载时调用它:-(@puropoix我知道现在发生了什么,它认为每个chrachter都是要添加文件名的元素?打开选项Strict。我不确定sshController是什么或做什么,但它要么返回一个文件列表,在这种情况下,For/each只是对一个文件进行N次迭代,要么它将单个文件解析为名称部分,在这种情况下,您仍然是将一个部分迭代N次。我看不到任何关于Arraylist“拆分为多个部分”的情况,这将导致一些新的数据结构
Arraylist
s已经过时,不应该再使用了。我们可以使用更好的东西,如
泛型
Dim lvi As ListViewitem

' go thru the list
For n As Integer = 0 to thisDocument.Count - 1
     lvi = new ListViewItem
     lvi.Text = thisDocument(n)
     ' or, since there are no subitems:
     ' lvi = New ListViewItem(thisDocument(n))

     lvAvailableDocuments.Items.Add(lvi)
Next n