我嘗試此源和視頻,但連接到FTP我連接時出錯。 FTP探險家:[http://developer.mobtool.net/VisualBasic/Internet/ftp-explorer.aspx][1]FTP Explorer錯誤vb.net
來源:
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
末級
錯誤:
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)
告訴你'index'參數在有效範圍之外,並且確切發生在哪裏。應該很明顯,首先要做的是找出'索引'使用的是什麼值,以及有效範圍是多少。 VS有一個調試器的原因。用它。即使您不知道如何使用斷點等,IDE仍會在拋出異常時爲您提供大量信息。不要忽視它。 – jmcilhinney