none
FTP Explorer error on connect vb.net RRS feed

  • Question

  • I try this source and video but on connect to ftp i get error on connecting. ftp explorer: http://developer.mobtool.net/VisualBasic/Internet/ftp-explorer.aspx

    Source:

    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
    End Class

    error:

    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)

    Sunday, August 13, 2017 7:37 AM

All replies

  • Hello,

    Which line is the exception thrown on?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, August 13, 2017 10:06 AM
    Moderator
  • Hello,

    Which line is the exception thrown on?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Line 11:

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


    • Edited by Ja7adR Sunday, August 13, 2017 12:25 PM
    Sunday, August 13, 2017 12:24 PM
  • Regardless of what you see in the TreeView there is nothing for Item(0) at the time of the button was clicked.

    For instance, if I have 

    tvServer.Nodes(0).Collapse works but if I use Nodes(1) I would get index out of bounds. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, August 13, 2017 12:53 PM
    Moderator
  • Regardless of what you see in the TreeView there is nothing for Item(0) at the time of the button was clicked.

    For instance, if I have 

    tvServer.Nodes(0).Collapse works but if I use Nodes(1) I would get index out of bounds. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    1 or 0 both get error not index ftp path on treeview

    Sunday, August 13, 2017 1:08 PM
  • Hello,

    Which line is the exception thrown on?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Line 11:

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



    Put a breakpoint in - step into the code and you'll probably see the issue yourself.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, August 13, 2017 1:18 PM
  • Hello,

    Which line is the exception thrown on?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Line 11:

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



    Put a breakpoint in - step into the code and you'll probably see the issue yourself.

    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Ja7adR Sunday, August 13, 2017 1:26 PM
    Sunday, August 13, 2017 1:25 PM
  • Javad,

    I won't be back for a few hours, but you see the exception's message so you know [about] what to to look for. You know the line of code that throws that exception so work through it.

    I'm not abandoning this, I just have to leave for a while is all.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Sunday, August 13, 2017 1:39 PM
  • Javad,

    I won't be back for a few hours, but you see the exception's message so you know [about] to to look for. You know the line of code that throws that exception so work through it.

    I'm not abandoning this, I just have to leave for a while is all.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    hi Frank 

    thanks for replay , for me important this source why don't work , i want know what is this Issue. 


    • Edited by Ja7adR Sunday, August 13, 2017 1:43 PM
    Sunday, August 13, 2017 1:43 PM

  • thanks for replay , for me important this source why don't work , i want know what is this Issue. 


    Sorry for the delay. Today is (was) our "take off the recycling stuff" day and I wanted to get loaded, get there, and get back before it got horribly hot out like it's been lately.

    Anyway - looking at that person's code, he's making a lot of assumptions. If you'll change the code for the button slightly, let's see what happens:

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

    Put a breakpoint on "tvServer.Nodes.Item(0).Collapse" and I bet that the breakpoint isn't reached. Am I right?

    It very well might end up throwing some other exception from some other place - I don't know that it will or not but let's see where that goes.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, August 13, 2017 2:58 PM

  • thanks for replay , for me important this source why don't work , i want know what is this Issue. 


    Sorry for the delay. Today is (was) our "take off the recycling stuff" day and I wanted to get loaded, get there, and get back before it got horribly hot out like it's been lately.

    Anyway - looking at that person's code, he's making a lot of assumptions. If you'll change the code for the button slightly, let's see what happens:

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

    Put a breakpoint on "tvServer.Nodes.Item(0).Collapse" and I bet that the breakpoint isn't reached. Am I right?

    It very well might end up throwing some other exception from some other place - I don't know that it will or not but let's see where that goes.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    thanks for help , next error on line 19:

     eTree.Nodes.Item(0).Nodes.Clear()

    Sunday, August 13, 2017 3:35 PM

  • thanks for help , next error on line 19:

     eTree.Nodes.Item(0).Nodes.Clear()

    Was I right that there were no nodes then?

    If so, that's probably what the problem is there but there are a lot of "assumptions" that I see in his code, including the fact that all FTP servers aren't set up the same. Getting directory info back may or may not work like he's assuming that it does.

    I'm not saying this to knock the author - but I hope you see my point? Be careful about what you find out there. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, August 13, 2017 3:39 PM