2010-02-23 34 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.