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

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


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