Secure FTP using C# and FTPS (FTP over SSL)
This article will demonstrate how using Secure FTP Factory for .NET you can establish a secure FTP session with an FTP server and perform various file operations. To see what else Secure FTP Factory for .NET has to offer Download a FREE 30 day Secure FTP Factory for .NET Evaluation.
 
Note: This example demonstrates using FTPS (FTP over SSL). If you are wanting to use the SFTP (FTP over SSH) protocol then please see the article Secure FTP using C# and SFTP (FTP over SSH).
Secure FTP Factory for .NET provides easy to use classes for communicating with an FTP server using the FTP or secure FTPS (AUTH TLS and Implicit SSL) protocols. The purpose of this tutorial is to demonstrate how to use the classes found in Secure FTP Factory for .NET. This tutorial is divided into the following topics:
- Creating a new
Ftp instance
- Subscribing to FTP events
- Establishing a connection
- Perform a remote directory listing
- Retrieving remote file data
- Downloading a file
- Uploading a file
- Releasing a connection
Before creating a new Ftp instance, ensure that the Jscape.Ftp namespace is defined in your using statements, and that the Jscape.Ftp.dll is referenced in your project. Refer to the Getting Started topic in the Secure FTP Factory for .NET User Guide for more information about adding the Jscape.Ftp.dll reference to your projects.
Create a new Ftp instance providing the server hostname, username, and password as arguments.
Ftp myFtp = new Ftp("hostname", "username", "password");
The Ftp class allows for client connections to an FTP server using FTP, AUTH TLS, or Implicit SSL protocols.
- FTP
- Client connects to server on port 21 using standard FTP.
- AUTH TLS
- Client connects to server on port 21 (the default FTP port) and issues a series of commands to establish a secure connection.
- Implicit SSL
- Client connects to server on port 990 and establishes a secure tunnel using SSL. No additional FTP commands are required to establish a connection.
Note
Consult your FTP server documentation to see which mode(s) your FTP server supports. Not all FTP servers support secure FTP connections using SSL. Of those that do support SSL some may only support one of the methods AUTH TLS or Implicit SSL.
To establish a secure connection to an FTP server, set the ConnectionType property. For secure connections you may connect using AUTH TLS or Implicit SSL. The default connection type is to connect using standard FTP. The example below demonstrates configuring the Ftp instance to connect using AUTH TLS.
Ftp myFtp = new Ftp("hostname", "username", "password");
myFtp.ConnectionType = Ftp.AUTH_TLS; // or use Ftp.IMPLICIT_SSL
You may capture FTP related events using the event classes found in Secure FTP Factory for .NET. To capture FTP events subscribe your Ftp instance to the events you wish to capture prior to invoking the Connect() method. This ensures that all data sent to and from the FTP server is captured by your event handling code.
// Subscribe to events
myFtp.FtpConnectedEvent += new FtpConnectEventHandler(OnConnected);
myFtp.FtpDisconnectedEvent += new FtpConnectEventHandler(OnDisconnected);
myFtp.FtpCommandEvent += new FtpCommandEventHandler(OnCommand);
myFtp.FtpResponseEvent +=new FtpCommandEventHandler(OnResponse);
myFtp.FtpDownloadEvent +=new FtpDownloadEventHandler(OnDownload);
myFtp.FtpUploadEvent +=new FtpDownloadEventHandler(OnUpload);
myFtp.FtpProgressEvent +=new FtpProgressEventHandler(OnProgress);
myFtp.FtpListingEvent +=new FtpListingEventHandler(OnListing);
myFtp.FtpConnectionLostEvent +=new FtpConnectEventHandler(OnConnectionLost);
Now you can create the event handler methods you previously defined when subscribing to the FTP events.
private void OnConnected(object sender, FtpConnectEventArgs e) {
Console.WriteLine("Ftp connected to " + e.HostName);
}
private void OnDisconnected(object sender, FtpConnectEventArgs e) {
Console.WriteLine("Disconnected " + e.HostName);
}
Once an Ftp instance has been created you may establish a connection to the FTP server by invoking the Connect() method.
myFtp.Connect();
The Ftp class contains methods to retrieve a remote directory listing. This example shows how to retrieve and display a directory listing as a string.
string dirList = myFtp.GetDirListingAsString();
Console.WriteLine("Remote Dir Listing\r\n" + dirList);
You can retrieve specific property information about the files in the remote directory using the FtpFile instance. This example shows how to retrieve and display some of the properties of the files in a remote directory listing.
IEnumerator e = myFtp.GetDirListing();
while (e.MoveNext()) {
FtpFile file = (FtpFile)e.Current;
Console.WriteLine("Filename: " + file.Filename);
if (file.IsDirectory) {
Console.WriteLine("\tDir Owner/Group: "+file.Owner + "/"+file.Group);
}else if(file.Link) {
Console.WriteLine("\tLink Target: "+file.LinkTarget);
}else{
Console.WriteLine("\tFile Size: "+file.Size);
Console.WriteLine("\tPermissions: "+file.Permission);
Console.WriteLine("\tModify Date: "+file.Date);
Console.WriteLine("\tModify Time: "+file.Time);
}
Console.WriteLine();
}
You can download one or more files from the current remote directory to your local directory. If you know the file name you want to download, use the Download() method. If you want to download files based on a file filter, use the MDownload() method.
This example uses the MDownload() method to download all files from the remote directory with a .log file extension. You may set the remote directory using the RemoteDir property.
// set remote dir to logs dir
myFtp.RemoteDir = "logs";
// download all log files
myFtp.MDownload("*.log");
As with downloading files, the Ftp class provides methods to upload one or more files from the current local directory to the current remote directory. If you know the file name you want to upload, use the Upload() method. If you want to upload files based on a file filter, use the MUpload() method.
This example uses the MUpload() method to upload all files from the local directory with a .jpg file extension to the images directory. You may set the remote directory using the RemoteDir property.
// set remote dir to site's images dir
myFtp.RemoteDir = "yoursite.com/images";
// upload all jpg files
myFtp.MUpload("*.jpg");
To release a connection simply invoke the Disconnect() method as follows:
myFtp.Disconnect();
The source code for this article is available for download and for viewing.
View example source
Download a FREE 30 day Secure FTP Factory for .NET Evaluation
|