Xceed .NET Libraries Documentation
Listing the contents of a local folder

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Task-Based Help > FTP capabilities > Working with classes from the Xceed.FileSystem namespace > Listing the contents of a local folder

The items contained in a folder can be listed using the GetItems, GetFiles, or GetFolders methods. With the Xceed FileSystem, a folder is a folder regardless of if it is located on a local disk, in a zip file, in memory, or on an FTP server. 

Using filters, the items that are listed can be limited by various criteria. 

The following example demonstrates how to list the contents of a local folder using the steps listed below:

  1. Create an instance of a DiskFolder which will represent the folder whose content to list. 

  2. Call the folder's GetFiles method to retrieve a listing of all the files contained in the folder and loop through the collection to print the list.

C# Copy Code

using Xceed.FileSystem;

DiskFolder folder = new DiskFolder( @"c:\temp" );

foreach( AbstractFile file in folder.GetFiles( true ) )
{
  System.Diagnostics.Debug.WriteLine( file.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. 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

Dim folder As New DiskFolder( "c:\temp" )
Dim events As New FileSystemEvents()

AddHandler events.ScanningFolder, AddressOf Me.scanning_folder

Dim file As AbstractFile
For Each file in folder.GetFiles( events, Nothing, True )
  System.Diagnostics.Debug.WriteLine( file.FullName )
Next file

RemoveHandler events.ScanningFolder, AddressOf Me.scanning_folder

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

C# Copy Code

using Xceed.FileSystem;

DiskFolder folder = new DiskFolder( @"c:\temp" );
FileSystemEvents events = new FileSystemEvents();

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

foreach( AbstractFile file in folder.GetFiles( events, null, true ) )
{
  System.Diagnostics.Debug.WriteLine( file.FullName );
}

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

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