2010-02-23 29 views
8

我已經實現了使用C#中的FtpWebRequest類上傳,下載,刪除等的功能。這非常簡單。如何在C#中發送任意FTP命令

我現在需要做的是支持直接從我們的app.config發送任意的FTP命令,如

quote SITE LRECL=132 RECFM=FB 
or 
quote SYST 

這裏是一個示例配置:

<!-- The following commands will be executed before any uploads occur --> 
<extraCommands> 
    <command>quote SITE LRECL=132 RECFM=FB</command> 
</extraCommands> 

我還在研究如何做到這一點使用FtpWebRequest。接下來我可能會嘗試WebClient。任何人都可以更快地將我指向正確的方向?謝謝!

更新: 我已經得出了同樣的結論,因爲.NET Framework 3.5的FtpWebRequest不支持除WebRequestMethods.Ftp.*以外的任何內容。我會嘗試一些其他帖子推薦的第三方應用程序。謝謝您的幫助!

+0

你有沒有用這些命令試試Rebex FTP?它工作正常嗎? – Askolein 2015-01-08 09:21:30

回答

9

我不認爲它可以與FtpWebRequest做......指定FTP命令的唯一方法是通過Method屬性和文檔狀態:

注意的是,在定義的字符串WebRequestMethods.Ftp類是Method屬性唯一支持的選項。將Method屬性設置爲任何其他值將導致ArgumentException異常。

的網站,SYST不是預定義的選項中,所以我想你就完蛋了......

不要浪費時間去嘗試的WebClient類,它會給你比FtpWebRequest甚至更​​少的靈活性。

然而,有大量的第三方FTP實現,開源或商業,我敢肯定,他們中的一些可以處理自定義命令...

3

您可以嘗試我們的Rebex FTP component

// create client and connect 
Ftp client = new Ftp(); 
client.Connect("ftp.example.org"); 
client.Login("username", "password"); 

// send SITE command 
// note that QUOTE and SITE are ommited. QUOTE is command line ftp syntax only. 
client.Site("LRECL=132 RECFM=FB"); 

// send SYST command 
client.SendCommand("SYST"); 
FtpResponse response = client.ReadResponse(); 
if (response.Group != 2) 
    ; // handle error 

// disconnect 
client.Disconnect(); 
-5

使用sendCommand("SITE LRECL=242 BLKSIZE=0 RECFM=FB");

+3

這是非常無益的,因爲沒有提供任何信息。什麼是「sendCommand」是第三方庫,它與FtpWebRequest有什麼關係? – 2012-07-21 17:08:29

6

FtpWebRequest不會幫助你Thomas Levesqueanswer說。你可以使用一些第三方解決方案或以下,簡化TcpClient基於代碼我從一個answer written in Visual Basic重構:

public static void SendFtpCommand() 
{ 
    var serverName = "[FTP_SERVER_NAME]"; 
    var port = 21; 
    var userName = "[FTP_USER_NAME]"; 
    var password = "[FTP_PASSWORD]" 
    var command = "SITE CHMOD 755 [FTP_FILE_PATH]"; 

    var tcpClient = new TcpClient(); 
    try 
    { 
     tcpClient.Connect(serverName, port); 
     Flush(tcpClient); 

     var response = TransmitCommand(tcpClient, "user " + userName); 
     if (response.IndexOf("331", StringComparison.OrdinalIgnoreCase) < 0) 
      throw new Exception(string.Format("Error \"{0}\" while sending user name \"{1}\".", response, userName)); 

     response = TransmitCommand(tcpClient, "pass " + password); 
     if (response.IndexOf("230", StringComparison.OrdinalIgnoreCase) < 0) 
      throw new Exception(string.Format("Error \"{0}\" while sending password.", response)); 

     response = TransmitCommand(tcpClient, command); 
     if (response.IndexOf("200", StringComparison.OrdinalIgnoreCase) < 0) 
      throw new Exception(string.Format("Error \"{0}\" while sending command \"{1}\".", response, command)); 
    } 
    finally 
    { 
     if (tcpClient.Connected) 
      tcpClient.Close(); 
    } 
} 

private static string TransmitCommand(TcpClient tcpClient, string cmd) 
{ 
    var networkStream = tcpClient.GetStream(); 
    if (!networkStream.CanWrite || !networkStream.CanRead) 
     return string.Empty; 

    var sendBytes = Encoding.ASCII.GetBytes(cmd + "\r\n"); 
    networkStream.Write(sendBytes, 0, sendBytes.Length); 

    var streamReader = new StreamReader(networkStream); 
    return streamReader.ReadLine(); 
} 

private static string Flush(TcpClient tcpClient) 
{ 
    try 
    { 
     var networkStream = tcpClient.GetStream(); 
     if (!networkStream.CanWrite || !networkStream.CanRead) 
      return string.Empty; 

     var receiveBytes = new byte[tcpClient.ReceiveBufferSize]; 
     networkStream.ReadTimeout = 10000; 
     networkStream.Read(receiveBytes, 0, tcpClient.ReceiveBufferSize); 

     return Encoding.ASCII.GetString(receiveBytes); 
    } 
    catch 
    { 
     // Ignore all irrelevant exceptions 
    } 

    return string.Empty; 
} 

可以期待以下流通過FTP獲取,同時:

220 (vsFTPd 2.2.2) 
user [FTP_USER_NAME] 
331 Please specify the password. 
pass [FTP_PASSWORD] 
230 Login successful. 
SITE CHMOD 755 [FTP_FILE_PATH] 
200 SITE CHMOD command ok.