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
- 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 - Create a new instance of the Ftp class.
- Register an event handler to the Listing event.
- 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") - 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") - Do your tasks such as browsing a directory, upload, download files...
- 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.