We help IT Professionals succeed at work.
Get Started

Trouble programmatically getting a list of the files on FTP server in VB.NET

715 Views
Last Modified: 2013-11-29
I am using the attached code in an attempt to generate a list of the files located in a particular directory on an FTP server.  However, when testing the code in debug mode, the code generates the following exception:

System.Net.WebException was unhandled
  Message="The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."

 

Some additional information that perhaps may be helpful: To manually doublecheck that my login credentials were working properly within VB, I CTRL + clicked on the link for the FTP directory within the VB project code.  I received a pop-up window that displays the same (550) error.  However, when I CTRL + click a second time, I successfully gain access to the directory.  This consistently only occurs on the second attempt, after receiving the 550 error on the first attempt.


Any help would be greatly appreciated.
Public Sub GetList()
        Dim strList As New List(Of String)
        Dim fwr As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri("ftp://servername.com/directoryname/directoryname/")), FtpWebRequest)
        fwr.Credentials = New NetworkCredential("xxxxxx", "xxxxxx")
        fwr.Method = WebRequestMethods.Ftp.ListDirectory
        Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())
        Dim str As String = sr.ReadLine()
        While str IsNot Nothing
            strList.Add(str)
            str = sr.ReadLine()
        End While
        sr.Close()
        sr = Nothing
        fwr = Nothing
    End Sub

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE