Xceed .NET Libraries Documentation
How to list specific files on an FTP server

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Task-Based Help > FTP capabilities > Working with classes from the Xceed.FileSystem namespace > How to list specific files on an FTP server

The content of a folder on an FTP server (or of any other type of folder supported by the Xceed FileSystem) can be listed using the GetItems, GetFiles, or GetFolders methods. Using filters, the items that are listed can be limited by various criteria. 

The following example demonstrates how to retrieve a listing of all ".txt" files located in an FTP server's current working directory using the steps listed below:

  1. Create an instance of the FtpConnection class to establish a connection between the client and the FTP server. If you are using FtpConnection in a UI application, assign your form (or any other control that implements the ISynchronizeInvoke interface) to the SynchronizingObject property and call Application.DoEvents in an event; see the Events section below for an example of this.

  2. Create an instance of an FtpFolder which will represent the folder on the FTP server whose content to list. If a folder name is not specified, the folder will represent the current working folder. 

  3. Call the FtpFolder's GetFiles method to retrieve a listing of all the ".txt" files contained in the FTP folder and loop through the collection to print the list. 

  4. Dispose of the FtpConnection once the operation is completed by calling its Dispose method or, in C#, by creating the FtpConnection instance in using blocks. If an instance of an FtpConnection object is not disposed of, connections with the FTP server may remain active until the FTP server times-out or the garbage-collector passes.

When dealing with case sensitive sources or destinations such as FTP servers, it is up to the user to pass a valid string to the method being called. For more information in case sensitivity, jump to the Case sensitivity topic.

VB.NET Copy Code

Imports Xceed.FileSystem
Imports Xceed.Ftp

Dim connection As FtpConnection

Try
  connection = New FtpConnection( "ftp.server.com" )

  Dim folder As New FtpFolder(connection)

  Dim item As AbstractFile
  For Each item In folder.GetFiles( True, New NameFilter( "*.txt" ) )
    System.Diagnostics.Debug.WriteLine( item.FullName )
  Next item

Finally
  connection.Dispose()
End Try

C# Copy Code
using Xceed.FileSystem;
using Xceed.Ftp;
 
using( FtpConnection connection = new FtpConnection( "ftp.server.com" ) )
{        
  FtpFolder folder = new FtpFolder( connection );
 
  foreach( AbstractFile item in folder.GetFiles( true, new NameFilter( "*.txt" ) ) )
  {
    System.Diagnostics.Debug.WriteLine( item.FullName );
  }
}

Events

All methods exposed by the Xceed FileSystem's FileSystemItem, AbstractFolder, AbstractFile, and derived classes have an overload that can be used when events are required. 

If you are using FtpConnection in a UI application, assign your form (or any other control that implements the ISynchronizeInvoke interface) to the SynchronizingObject property and call Application.DoEvents in an event.

With the exception of the FtpConnection's ParsingListingLine event, events can be handled by creating an instance of the FileSystemEvents class and subscribing to the desired events. For example:

VB.NET Copy Code

Imports Xceed.FileSystem
Imports Xceed.Ftp

Dim connection As FtpConnection
Dim events As FileSystemEvents

Try
  connection = New FtpConnection( "ftp.server.com" )
  'When using FtpConnection in a UI application
  connection.SynchronizingObject = Me

  events = New FileSystemEvents
  AddHandler events.ScanningFolder, AddressOf Me.scanning_folder

  Dim folder As New FtpFolder(connection)

  Dim item As AbstractFile
  For Each item In folder.GetFiles( events, Nothing, True, New NameFilter( "*.txt" ) )
    System.Diagnostics.Debug.WriteLine( item.FullName )
  Next item

Finally
  connection.Dispose()
  RemoveHandler events.ScanningFolder, AddressOf Me.scanning_folder
End Try

Private Sub scanning_folder(ByVal sender As Object, ByVal e As ScanningFolderEventArgs)
  Application.DoEvents()
  System.Diagnostics.Debug.WriteLine(e.CurrentItem.Name)
End Sub

C# Copy Code

using Xceed.FileSystem;
using Xceed.Ftp;

using( FtpConnection connection = new FtpConnection( "ftp.server.com" ) )
{         
  //When using FtpConnection in a UI application
  connection.SynchronizingObject = this;

  FileSystemEvents events = new FileSystemEvents();
  events.ScanningFolder += new ScanningFolderEventHandler( this.scanning_folder );

  FtpFolder folder = new FtpFolder( connection );

  foreach( AbstractFile item in folder.GetFiles( events, null, true, new NameFilter( "*.txt" ) ) )
  {
    System.Diagnostics.Debug.WriteLine( item.FullName );
  }

  events.ScanningFolder -= new ScanningFolderEventHandler( this.scanning_folder );
}

private void scanning_folder( object sender, ScanningFolderEventArgs e )
{
  Application.DoEvents();
  System.Diagnostics.Debug.WriteLine( e.CurrentItem.Name );
}

Things to consider