Listing Event of the Ftp Class

Listing event is triggered when a file and directory listing item has been received by ListDirectory, ListRawName or ListName methods. By handling this event, you can display information of the received FTP item or even parse the raw data of the received item line.

The following steps guide you on how to handle this event.

Handling Listing event

  1. Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
    C#  
    using Atp.Net; 
    VB.NET  
    Imports Atp.Net
  2. Create a new instance of the Ftp class.
  3. Register an event handler to the Listing event.
  4. Now you can connect to the FTP server with Connect methods. The code looks similar to the following:
    C#  
    // Create a new instance.
    Ftp client = new Ftp();
    // Register an event handler.
    client.Listing += client_Listing;
    // Connect to the FTP server.
    client.Connect("localhost");
    VB.NET  
    ' Create a new instance.
    Dim client As New Ftp()
    ' Register an event handler.
    AddHandler client.Listing, AddressOf client_Listing
    ' Connect to the FTP server.
    client.Connect("localhost")
  5. Use your user name and password to login with Authenticate methods. The code looks similar to the following:
    C#  
    // Authenticate.
    client.Authenticate("userName", "password");
    VB.NET  
    ' Authenticate.
    client.Authenticate("userName", "password")
  6. Do your tasks such as browsing a directory, upload, download files...
  7. After completing your work, call the Disconnect method to close the FTP session. 

Final example code

C#  
public void HandleListingEvent()
{
   
// Create a new instance.
   
Ftp client = new Ftp();
   client.Listing += client_Listing;
   
// Connect to the FTP server.
   
client.Connect("localhost");
   
// Or you can specify the FTP port with
   
// client.Connect("myserver", 21);
   
// Authenticate.
   
client.Authenticate("test", "test");  
   
// ...
   
// Get file list.
   
client.ListDirectory("");  
   
// ...
   
// Disconnect.
   
client.Disconnect();
}
void client_Listing(object sender, FtpListingEventArgs e)
{
   Console.WriteLine(
"Received raw line: " + e.RawData);
   Console.WriteLine(
"FTP file: " + e.File.ToString());
}
VB.NET  
Public Sub HandleListingEvent()
    ' Create a new instance.
    Dim client As New Ftp()
    AddHandler client.Listing, AddressOf client_Listing
    ' Connect to the FTP server.
    client.Connect("localhost")
    ' Or you can specify the FTP port with
    ' client.Connect("myserver", 21);
    ' Authenticate.
    client.Authenticate("test", "test")
    ' ...
    ' Get file list.
    client.ListDirectory("")
    ' ...
    ' Disconnect.
    client.Disconnect()
End Sub
Private Sub client_Listing(ByVal sender As Object, ByVal e As FtpListingEventArgs)
    Console.WriteLine("Received raw line: " & e.RawData)
    Console.WriteLine("FTP file: " & e.File.ToString())
End Sub

 

Click here to download the Ultimate FTP Component for .NET, or here to download the .NET CF version.

Security Overview and How to Connect to FTP/SSL Servers

This topic gives you an overview of Certificate and Security Modes for FTP/SSL. It also illustrates how to connect to FTP/SSL servers using Ultimate FTP.

What is a digital certificate?

Authentication is important for secure communications. Users must be able to prove their identity to the entities they are communicating with. In addition, they must be able to verify the identity of the entity communicating with them. This is accomplished by presenting or verifying some form of trusted credentials.

A digital certificate is a common credential that provides authentication. A trusted organization, called a Certificate Authority (CA), assigns a certificate to a user or entity and the user or entity then uses the certificate to prove itself to the other side. You may configure your system to accept any number of Certificate Authorities but ATP does not recommend this configuration. Completing the following steps to access the Certificates dialog box:

  1. In the Control Panel, double-click Internet Options.
  2. Select the Content tab, and then click Certificates.

Where do I get a certificate?

Certificates must come from a trusted CA. A user submits a certificate request to a CA and the CA returns a certificate for the user to use. Certificate used on our FTP server is generated by FileZila and CuteFTP. Listed below are some Certificate Authorities:

  • Microsoft Certificate Server - used for internal corporate Certificate delegation
  • VeriSign - (www.verisign.com) provides certificates for Internet users and servers

You always need a digital certificate installed to operate as a server. You only need a digital certificate installed on a client if the server requests authentication.

How does the authentication process work?

  1. The client normally initiates the TCP connection. The Connect method makes this TCP connection, sends a "client hello" message, and automatically responds to authentication requests received from the server.
  2. The server accepts the connection initiated by the client, constructs its digital credentials from the digital certificate referred to by the Server.Certificate Property, and sends its credentials to the client.
  3. Optionally, the server may also send an authentication request to the client.
  4. The client receives the server's credentials, verifies it against the CAs it is configured to trust, and responds with its own credentials if an authentication request is received.
  5. If the client and server cannot negotiate a mutually acceptable security protocol, an error is generated and the connection is closed.
  6. If the client cannot validate the server's credentials, an error is generated and the client closes the connection.
  7. If the server cannot validate the client's credentials (if requested), an error is generated and the server closes the connection.
  8. Once protocol negotiation and authentication is successful, secure encryption/decryption is performed on all data passing over the connection.
  9. The session is terminated when one side closes the connection.

What is a certificate store?

A certificate store is a location on the system (memory, disk, registry, etc.) where certificates are stored for use. There are three major system stores and other minor stores. The three major stores are:

  • MY - personal certificates go here. (e.g. ATP code signing)
  • ROOT - certificates for Trusted Root Certificate Authorities.
  • CA - all other certificates.

What are the system store and the machine store?

The system store is the certificate store located in the HKEY_CURRENT_USER registry key. The machine store is the certificate store located in the HKEY_LOCAL_MACHINE registry key. Applications installed as a service should store their certificates in the machine store since there is no current user when running as a service.

What is the X500 naming convention?

This is a format for creating a distinguished name. The different parts of the name are described below:

  • C - country you are in (ex. US).
  • S - state you are in (ex. California).
  • L - locality value or city (ex. Walnut).
  • O - your organization (ex. ATP, Inc.).
  • OU - organizational unit (ex. Software Development).
  • CN - common name; typically the name of the system or user (Ex. www.mydomain.com).

Here are some examples of X500 names:

  • C=US, S=New York, L=Syracuse, O=ATP, Inc., OU=ATP, CN=My Machine
  • C=US, S=Georgia, L=Atlanta, O=MyOrg, OU=Toy Department, CN=John Doe

Explicit connection

When the client connects to the server using SSL, an SSL negotiation is initialized, the connection is secured and all following communication is being protected. 

The code snippet below shows how to connect to an FTP server securely using Explicit security mode.

C#  
// Create a new instance.
Ftp client = new Ftp();
// Connect to the FTP server.
client.Connect("myserver", 21, SecurityMode.Explicit);
// Authenticate.
client.Authenticate("userName", "password");
// Do something here...
client.DownloadFile("/my remote file.dat", "my local file");
// Disconnect.
client.Disconnect();

 

VB.NET

 
' Create a new instance.
Dim client As New Ftp()
' Connect to the FTP server.
client.Connect("myserver", 21, SecurityMode.Explicit)
' Authenticate.
client.Authenticate("userName", "password")
' Do something here...
client.DownloadFile("/my remote file.dat", "my local file")
' Disconnect.
client.Disconnect()

 

Implicit Connection with FTP

Originally, a separate port was assigned to the SSL version of the FTP. The port number 990 is usually assigned for FTP/SSL. Upon connection to this port, an SSL negotiation starts immediately and the control connection is secured. All data connections are also secured implicitly in the same way. This is similar to the approach used by HTTPS.

The code snippet below shows how to connect to an FTP server securely using Implicit security mode in Ultimate FTP.

C#  
// Create a new instance.
Ftp client = new Ftp();
// Connect to the FTP server.
client.Connect("myserver", 990, SecurityMode.Implicit);
// Authenticate.
client.Authenticate("userName", "password");
// Do something here...
client.DownloadFile("/my remote file.dat", "my local file");
// Disconnect.
client.Disconnect();

VB.NET  
' Create a new instance.
Dim client As New Ftp()
' Connect to the FTP server.
client.Connect("myserver", 990, SecurityMode.Implicit)
' Authenticate.
client.Authenticate("userName", "password")
' Do something here...
client.DownloadFile("/my remote file.dat", "my local file")
' Disconnect.
client.Disconnect()

Click here to download the Ultimate FTP Component for .NET, or here to download the .NET CF version.