Issue a custom command to FTP server

This article describes how to issue a custom command to FTP server using Ftp.dll – .NET FTP and FTPS library.

// C# version

using (Ftp client = new Ftp())
{
    client.Connect("ftp.example.org");
    client.Login("user", "password");

    FtpResponse response = client.SendCommand("HELP");

    Console.WriteLine(response.Message);
    foreach (string line in response.Lines)
        Console.WriteLine(line);

    client.Close();
}

' VB.NET version

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	Dim response As FtpResponse = client.SendCommand("HELP")

	Console.WriteLine(response.Message)
	For Each line As String In response.Lines
		Console.WriteLine(line)
	Next

	client.Close()
End Using

The abowe code producess following output:


The following commands are recognized:
USER PASS QUIT CWD PWD PORT PASV TYPE
LIST REST CDUP RETR STOR SIZE DELE RMD
MKD RNFR RNTO ABOR SYST NOOP APPE NLST
MDTM XPWD XCUP XMKD XRMD NOP EPSV EPRT
AUTH ADAT PBSZ PROT FEAT MODE OPTS HELP
ALLO MLST MLSD SITE P@SW STRU CLNT MFMT
HASH

The other overload of SendCommand allows you to specify, if after negative response exception is thrown.

The next sample will not throw exception if the server does not recognize the command.
You can examine IsPositive property to check if the response was successful or not.

// C# version

using (Ftp client = new Ftp())
{
    client.Connect("ftp.example.org");
    client.Login("user", "password");

    FtpResponse response = client.SendCommand("HELP", false);

    Console.WriteLine(response.IsPositive);
    Console.WriteLine(response.Message);
    foreach (string line in response.Lines)
        Console.WriteLine(line);


    client.Close();
}
' VB.NET version

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	Dim response As FtpResponse = client.SendCommand("HELP", False)

	Console.WriteLine(response.IsPositive)
	Console.WriteLine(response.Message)
	For Each line As String In response.Lines
		Console.WriteLine(line)
	Next


	client.Close()
End Using

You can download Ftp.dll FTP/FTPS client for .NET here.

Tags:    

Questions?

Consider using our Q&A forum for asking questions.