0

I am trying to get a list of files on a server

CODE:

string ftpUserID = "user";
string ftpPassword = "password";
string ftpServerIP = "192.###.###.###";
string remoteDirectory = @"\Update\UpdateTest";
string localDirectory = @"C:\Updates";

string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" +      remoteDirectory));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
reqFTP.EnableSsl = true;
reqFTP.Proxy = null;
reqFTP.KeepAlive = true;
reqFTP.UsePassive = true;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();
while (line != null)
{
    result.Append(line);
    result.Append("\n");
    line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');

I keep getting the WebException error saying 'Unable to connect to the remote server'

This is as a result of errors in :

 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" +      remoteDirectory));

Which is throwing a System.NotSuportedException on the reqFTP.Content and reqFTP.PreAuthenticate parts I think.

Daniel Bidulock
  • 2,344
  • 1
  • 26
  • 27
Noelle
  • 772
  • 9
  • 18
  • 44

1 Answers1

0

You mention, though it is not visible in code, that you use PreAuthenticate. This is not supported for FTP.

The PreAuthenticate property is provided only for compatibility with other implementations of the WebRequest and WebResponse classes, as per msdn.

dove
  • 20,469
  • 14
  • 82
  • 108