Link to home
Start Free TrialLog in
Avatar of Jorhal
Jorhal

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Skylinc
Skylinc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Skylinc
Skylinc

Could you maybe tell us what the problem was? Just out of curiosity...
Avatar of Jorhal

ASKER

After reading your post, I followed your example and tested the code successfully on a different FTP server.  So I realized the issue was specific to the server I was trying to access -- it seems like it had something to do with the directory location.

On the targeted FTP server, I was trying to access the directory: ftp://domain.com/ClientName/dir1/

That link would worked okay in Internet Explorer, but did not work in my code for some reason.  However, I tried ftp://domain.com/  -- this work successfully in my code, and automatically placed me in the "/ClientName" directory upon login.    
It is possible that the ClientName folder is located slightly deeper in the structure, and when the login was set up, it was directed to the correct folder. Example: ftp://domain.com/public_ftp/clients/ClientName.

I am glad that your problem was solved though!