Using FtpWebRequest

The following code example illustrates how to use the FtpWebRequest class to download a remote file from an FTP server.

C#:

// Register FtpWebRequest for the specified schema.
WebRequest.RegisterPrefix("ftp://", Atp.Net.FtpWebRequest.Creator);
Console.WriteLine("Sending request...");
// Create a WebRequest for the specified URL.
WebRequest request = WebRequest.Create("ftp://ftp.example.net/pub/myfile.zip");
// Send the WebRequest and waits for a response.
WebResponse response = request.GetResponse();
// Get remote file stream for downloading.
Stream remoteFileStream = response.GetResponseStream();
Stream localFileStream = File.Create("myfile.zip");
// Create a new buffer to download.
byte[] buffer = new byte[1024];
int n;
do
{
   // Read data from the remote file stream.
   n = remoteFileStream.Read(buffer, 0, buffer.Length);
   // Write to the local file stream.
   localFileStream.Write(buffer, 0, n);
} while (n > 0);
Console.WriteLine("Response Received.");
localFileStream.Close();
// Release the resources of the response.
remoteFileStream.Close();

VB.NET:

' Register FtpWebRequest for the specified schema.
WebRequest.RegisterPrefix("ftp://", Atp.Net.FtpWebRequest.Creator)
Console.WriteLine("Sending request...")
' Create a WebRequest for the specified URL.
Dim request As WebRequest = WebRequest.Create("ftp://ftp.example.net/pub/myfile.zip")
' Send the WebRequest and waits for a response.
Dim response As WebResponse = request.GetResponse()
' Get remote file stream for downloading.
Dim remoteFileStream As Stream = response.GetResponseStream()
Dim localFileStream As Stream = File.Create("myfile.zip")
' Create a new buffer to download.
Dim buffer(1023) As Byte
Dim n As Integer
Do
    ' Read data from the remote file stream.
    n = remoteFileStream.Read(buffer, 0, buffer.Length)
    ' Write to the local file stream.
    localFileStream.Write(buffer, 0, n)
Loop While n > 0
Console.WriteLine("Response Received.")
localFileStream.Close()
' Release the resources of the response.
remoteFileStream.Close()

Sending custom commands to an FTP server

Explicitly sending commands to an FTP server is usually not necessary. Almost all commands that could conceivably be sent to an FTP server are encapsulated by high-level methods. However, to send a command to an FTP server, simply call the SendCommand method, and to get the response from the FTP server, call the ReadResponse method.

The example below sends the "FEAT" command to an FTP server and prints out the response:

C#:

// Create a new Ftp instance.
Ftp client = new Ftp();
// Connect to the FTP server.
client.Connect("server");
// Authenticate.
client.Authenticate("username", "password");
// Send command
client.SendCommand("FEAT");
// Print out the response
FtpResponse res = client.ReadResponse();
Console.WriteLine(res.RawResponse);
// Disconnect.
client.Disconnect();

VB.NET

' Create a new FtpClient instance.
Dim client As New Ftp()
' Connect to the FTP server.
client.Connect("server")
' Authenticate.
client.Authenticate("username", "password")
' Send command
client.SendCommand("FEAT")
' Print out the response
Dim res As FtpResponse = client.ReadResponse()
Console.WriteLine(res.Text)
' Disconnect.
client.Disconnect()