Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iTunes Gift Card Offer

For a limited time get a $50.00 iTunes gift card free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 7.0 Released
08/20/2010 03:31 AM

JSCAPE launches Employee Giving Program
07/03/2010 08:26 AM

SSH Factory 3.6 Released
07/03/2010 06:22 AM

Secure iNet Factory 8.5 Released
07/03/2010 06:07 AM

Secure FTP Factory 8.5 Released
07/03/2010 06:03 AM

Secure FTP Applet 6.2 Released
06/10/2010 02:23 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Open up corporate data to your partners
08/03/2010 10:01 PM

Access vital corporate documents on the go
07/01/2010 02:15 PM

SFTP and Encryption
05/17/2010 09:52 PM

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 AM

more...


Feedback

Request a feature or component

Request a Java or .NET component


 

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.

ftp .net downloadftp .net purchase

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).

Overview of Secure FTP Factory for .NET

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:

  1. Creating a new Ftp instance
  2. Subscribing to FTP events
  3. Establishing a connection
  4. Perform a remote directory listing
  5. Retrieving remote file data
  6. Downloading a file
  7. Uploading a file
  8. Releasing a connection

Creating a new Ftp instance

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


Subscribing to FTP Events

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);
}


Establishing a connection

Once an Ftp instance has been created you may establish a connection to the FTP server by invoking the Connect() method.

myFtp.Connect();


Perform a remote directory listing

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);


Retrieving remote file data

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();
}


Downloading a file

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");


Uploading a file

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");

 

Releasing a connection

To release a connection simply invoke the Disconnect() method as follows:

myFtp.Disconnect();

Examples

The source code for this article is available for download and for viewing.

View example source

Download

Download a FREE 30 day Secure FTP Factory for .NET Evaluation