Issue a custom command to FTP server

In this article, we are going to show some examples that send FTP commands to a remote FTP server using the the Ultimate FTP Component. Basically, the FTP protocol defines a set of standard commands which governs the communications between the client and the server.

The client sends a command and the server returns a response either via the control channel or data channel (if a data connection is established). Behind the scenes, the Ultimate FTP library encapsulates and hides all these command-level details in order to expose high-level interfaces to the users. Therefore, it’s very rarely that we need to send the raw FTP commands.

However, the Ultimate FTP still provides such functionality if needed, e.g. in cases of some FTP servers define their own commands other than the standard ones.

Send Custom Command to FTP Server

Typically, the syntax for sending a FTP command from the client looks like this:

CommandName <argument_1> <argument_2>, …

For example, the following command creates a directory named “MyFolder”:

MKD MyFolder

If the operation succeeds, the server returns a response looks like this:

257 "/MyFolder" created successfully

Depending on the type of command, the server returns the response either via control channel or data channel (e.g. file transfer commands). For simplicity, this article only deals with the response sent through the control channel, which can be retrieved via ReadResponse() method of the Ftp class.

To send a FTP command, we can use the SendCommand() methods provided by the UltimateFTP.

For example, the following code snippet sends a MKD command to create a directory named “MyFolder”:

C#:

// Create a new class instance
using (Ftp client = new Ftp())
{
	// Connect to the FTP server
	client.Connect("myserver");

	// Authenticate
	client.Authenticate("user", "password");


	//Send Command      
	client.SendCommand(“MKD MyFolder”);

	//Printout the response
	FtpResponse res = client.ReadResponse();
	Console.WriteLine(res.RawResponse);

	// Disconnect
	client.Disconnect();
}

VB.NET:

' Create a new class instance.
Using client As New Ftp()

    ' Connect to the FTP server.
    client.Connect("myserver")

    ' Authenticate
    client.Authenticate("user", "password")


    ' Send Command
    client.SendCommand("MKD MyFolder")

    ' Printout the response
    Dim res As FtpResponse = client.ReadResponse()
    Console.WriteLine(res.RawResponse)

    ' Disconnect.
    client.Disconnect()
End Using

The above code produces following output:

  • If folder created successfully:
    257 "/MyFolder" created successfully
  • If folder already exist then:
    550 Directory already exists

Validating Response for Custom Command Sent to Server

You can validate the response code received upon execution of command send to FTP Server. Following code shows how to validate the response.

C#:

// Create a new class instance
using (Ftp client = new Ftp())
{
	// Connect to the FTP server
	client.Connect("myserver");

	// Authenticate
	client.Authenticate("user", "password");


	//Send Command      
	client.SendCommand(“MKD MyFolder”);

	//Printout the response
	FtpResponse res = client.ReadResponse();
	if(res.Code != 257)
	Console.WriteLine(“Error in creating directory”);
	else
	Console.WriteLine(res.RawResponse);

	// Disconnect
	client.Disconnect();
}

VB.NET:

' Create a new class instance.
Using client As New Ftp()

    ' Connect to the FTP server.
    client.Connect("myserver")

    ' Authenticate
    client.Authenticate("user", "password")


    ' Send Command
    client.SendCommand("MKD MyFolder")

    ' Printout the response
    Dim res As FtpResponse = client.ReadResponse()
    If Not res.Code = 257
	Console.WriteLine(“Error in creating directory”)
    Else
    	Console.WriteLine(res.RawResponse)

    ' Disconnect.
    client.Disconnect()
End Using

Note: Refer reference section for details about various return codes

References

RFC 959

List of FTP Commands

List of FTP Return Codes

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now

Dont miss out Get update on new articles and other opportunities