2012-05-04 99 views
0

我想獲得一個服務器上的文件列表下載文件

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'); 

我不斷收到引發WebException錯誤說「無法連接到遠程服務器」

這是錯誤的結果:

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

這是拋出一個System.NotSuportedException on reqFTP.Content and reqFTP.PreAuthenticate部分我想。

+0

嘗試'reqFTP.EnableSsl = false;'? –

+0

僅供參考 - 您的最終FTP網址中有前後斜槓。 –

回答

0

您提到,雖然它在代碼中不可見,但您使用的是PreAuthenticate。這不支持FTP。

PreAuthenticate屬性僅用於與WebRequest和WebResponse類的其他實現兼容,如msdn所示。