Secure FTP using Visual Basic 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 with 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.
After you have the Jscape.Ftp.dll reference added to your project, you can define an Ftp instance with Events.
Public WithEvents myFtp As Jscape.Ftp.Ftp
Now you can create a new Ftp instance providing the server hostname, username, and password as arguments.
myFtp = New Jscape.Ftp.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.
myFtp.ConnectionType = Jscape.Ftp.Ftp.AUTH_TLS ' or use Jscape.Ftp.Ftp.IMPLICIT_SSL
Now you can create the event handler methods for the ftp events.
Public Sub OnConnected(ByVal sender As Object, ByVal e As FtpConnectEventArgs) Handles myFtp.FtpConnectedEvent
System.Console.WriteLine("Connected: " & e.HostName)
End Sub
Public Sub OnDisconnected(ByVal sender As Object, ByVal e As FtpConnectEventArgs) Handles myFtp.FtpDisconnectedEvent
System.Console.WriteLine("Disconnected: " + e.HostName)
End Sub
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.
Dim dirList As String = myFtp.GetDirListingAsString()
System.Console.WriteLine("Remote Dir Listing" + vbCrLf + 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.
Dim e As IEnumerator = myFtp.GetDirListing()
While e.MoveNext()
Dim file As FtpFile = e.Current
System.Console.WriteLine(file.Filename)
If file.IsDirectory Then
System.Console.WriteLine("Dir Owner/Group: " + file.Owner + "/" + file.Group)
ElseIf (file.Link) Then
System.Console.WriteLine(vbTab + "Link Target: " + file.LinkTarget)
Else
System.Console.WriteLine(vbTab + "File Size: " + file.Size)
System.Console.WriteLine(vbTab + "Permissions: " + file.Permission)
System.Console.WriteLine(vbTab + "Modify Date: " + file.Date)
System.Console.WriteLine(vbTab + "Modify Time: " + file.Time)
End If
System.Console.WriteLine()
End While
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
|