Using SITE and Other Custom Commands with the FTP Component
FTP servers often support commands beyond the standard set used by the FTP component's methods, including site-specific commands that are sent using the SITE command. The FTP component can send any of these commands to the server using the SendCommand method, which sends the command exactly as it is provided. This is the equivalent of the QUOTE (or literal) command in command-line FTP clients, which sends its arguments directly to the server.
Sending a Command
To send a command, call SendCommand after logging on. The command is passed without the QUOTE prefix, since QUOTE is a feature of command-line clients rather than a command that the server understands. After the command completes, the final line of the server's reply is available in the LastReply property.
FTP ftp = new FTP();
ftp.RemoteHost = "ftp.example.com";
ftp.User = "USER";
ftp.Password = "PASSWORD";
ftp.Logon();
// Equivalent to "QUOTE SITE UMASK 022" in a command-line FTP client.
ftp.SendCommand("SITE UMASK 022");
Console.WriteLine("Server response: " + ftp.LastReply);
ftp.Logoff();
If the server rejects the command (for example, with a 500 reply because the command is not recognized, or a 502 reply because it is not implemented), SendCommand throws an exception with error code 141, and the exception's message includes the server's reply.
try {
ftp.SendCommand("SITE IDLE 600");
} catch (IPWorksException ex) {
Console.WriteLine("The server rejected the command: " + ex.Message);
}
Discovering Supported Commands
The site-specific commands that a server supports vary from server to server. Many servers return a list of them in response to the HELP SITE command, and the FEAT command returns a list of the optional standard features that the server supports.
The replies to these commands typically span multiple lines. Since LastReply only contains the final line of the reply, use the PITrail event to capture the full reply. The event fires for each line sent to and received from the server, and its Direction parameter is 1 for lines received from the server.
StringBuilder reply = new StringBuilder();
ftp.OnPITrail += (s, e) => {
if (e.Direction == 1) reply.AppendLine(e.Message); // 1 = a line received from the server.
};
reply.Clear();
ftp.SendCommand("HELP SITE");
Console.WriteLine(reply.ToString());
Notes
- Commonly supported SITE commands include SITE CHMOD (change permissions), SITE UMASK (set the default permissions for new files), and SITE IDLE (set the idle timeout), but their availability and syntax depend on the server. For changing permissions, please see our Setting File Permissions with the FTP Component entry.
- Commands that transfer data over a separate data connection (such as LIST, RETR, and STOR) should not be sent with SendCommand. Use the corresponding methods (such as ListDirectoryLong, Download, and Upload) instead.
- The same approach works for FTPS (FTP over TLS), which is supported by the FTP component through the SSLStartMode property.
Note that in older versions of IPWorks, commands were sent by setting the Command property, which has been replaced by the SendCommand method.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.