VB.NET FTP 资源管理器

VB.NET FTP Explorer

我尝试使用此源和视频,但在连接到 ftp 时出现连接错误。

来源:

Imports System.IO
Imports System.Net


Public Class MainForm
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtServer.Select()
    End Sub

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    tvServer.Nodes.Item(0).Collapse()
    LoadFTPRemote(txtServer.Text, tvServer)
End Sub

Public Sub LoadFTPRemote(ByVal dirpath As String, eTree As TreeView)
    eTree.Nodes.Item(0).Nodes.Clear()
    eTree.BeginUpdate()
    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(dirpath), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.ListDirectory
    request.Credentials = New NetworkCredential(txtUsername.Text, txtPassword.Text)
    Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
    Dim responseStream As Stream = response.GetResponseStream()
    Dim reader As New StreamReader(responseStream)
    Do While reader.Peek() >= 0
        Dim nD As TreeNode = New TreeNode(reader.ReadLine())
        eTree.Nodes.Item(0).Nodes.Add(nD)
    Loop
    reader.Close()
    response.Close()
    eTree.Nodes.Item(0).Expand()
    eTree.EndUpdate()
End Sub

Private Sub tvServer_AfterExpand(sender As Object, e As TreeViewEventArgs) Handles tvServer.AfterExpand
    tvServer.BeginUpdate()
    Dim Node As TreeNode = e.Node
    For Each CNode As TreeNode In Node.Nodes
        Dim PathBuilder As String = txtServer.Text & ProperRemoteLink(CNode.FullPath)
        If Not Path.HasExtension(PathBuilder) Then
            CNode.Nodes.Clear()
            Dim request As FtpWebRequest = DirectCast(WebRequest.Create(PathBuilder), FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.ListDirectory
            request.Credentials = New NetworkCredential(txtUsername.Text, txtPassword.Text)
            Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
            Dim responseStream As Stream = response.GetResponseStream()
            Dim reader As New StreamReader(responseStream)
            Dim strList As New List(Of String)
            Do While reader.Peek() >= 0
                Dim xNod As TreeNode = New TreeNode(IO.Path.GetFileName(reader.ReadLine()))
                CNode.Nodes.Add(xNod)
            Loop
            reader.Close()
            response.Close()
        End If
    Next
    tvServer.EndUpdate()
End Sub

Public Function ProperRemoteLink(ByVal input As String) As String
    Dim output As String = input
    If input.StartsWith("FTP Server") Then output = input.ToString.Remove(0, 10)
    Return output
End Function

Private Sub tvServer_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles tvServer.AfterSelect
    lblStatus.Text = txtServer.Text & ProperRemoteLink(e.Node.FullPath)
End Sub

结束Class

错误:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index at System.Windows.Forms.TreeNodeCollection.get_Item(Int32 index) at zbmsdn.MainForm.btnLogin_Click(Object sender, EventArgs e) in c:\users\javad\documents\visual studio 2017\Projects\zbmsdn\zbmsdn\MainForm.vb:line 11 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)

您应该在执行 (0) 之前尝试检查是否选择了节点...

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    If tvServer.Nodes.Count > 0 Then
        tvServer.Nodes.Item(0).Collapse()
        LoadFTPRemote(txtServer.Text, tvServer)
    End If
End Sub

这就是我认为的问题所在,但是您提供的代码又非常有限。