Xceed .NET Libraries Documentation
ItemException event

Welcome to Xceed .NET, .NET Standard and Xamarin Libraries! > Basic Concepts > Events > ItemException event

The ItemException event is raised when an exception is caught during the processing of a list of FileSystemItem objects, allowing the handler to decide if the process should be aborted and the exception be thrown, if the process should continue with the next item in the list or if the same item should be retried.

Purpose

The ItemException event is used to intercept exceptions before they are thrown. This can be useful in the case where you are copying a large number of files and an error occurs on one of the files or if you are trying to access a file that is password protected. In both of these cases, custom handling of the exception can be added to the event handler, or if no special handling is required, a message could be displayed explaining the reason why the exception was thrown. 

The following methods can raise the ItemException event: 

Method Description
FileSystemItem.CopyTo Copies the item to another folder.
FileSystemItem.Delete Permanently deletes the physical item.
FileSystemItem.MoveTo Moves the item to another folder.
AbstractFolder.CopyFilesTo Copies the folder's content to another folder.
AbstractFolder.GetFiles Returns a filtered list of files contained in the folder.
AbstractFolder.GetFolders Returns a filtered list of folders contained in the folder.
AbstractFolder.GetItems Returns a list of items contained in the folder.
AbstractFolder.MoveFilesTo Moves the folder's content to another folder.
AbstractFile.OpenRead Opens the file for reading.
AbstractFile.OpenWrite Opens the file for writing.

Basic steps - C#

To subscribe to the ItemException event, the following steps must be performed:

  • Create a reference to a FileSystemEvents object. 

  • Subscribe to the ItemException event of the FileSystemEvents object using the ItemExceptionEventHandler delegate class. 

  • Create a new method that will handle the events that are raised. For the purposes of this example, we will call the method OnItemException. 

  • Place the desired code in the newly created event handler.

Basic steps - VB.NET

To subscribe to the ItemException event, the following steps must be performed:

Demonstration

This example demonstrates how to use the ItemException event to delete a target file that already exists.

VB.NET Copy Code

Imports Xceed.FileSystem

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

folder.CopyFilesTo( fiEvents, Nothing, New DiskFolder( "c:\destination" ), True, False )  

Private Sub fiEvents_ItemException(ByVal sender As Object, _
                                   ByVal e As Xceed.FileSystem.ItemExceptionEventArgs) Handles fiEvents.ItemException

  If (TypeOf e.Exception Is ItemAlreadyExistsException) Then
    e.TargetItem.Delete()
    e.Action = ItemExceptionAction.Retry
  End If
End Sub

C# Copy Code
using Xceed.FileSystem;
 
FileSystemEvents fiEvents = new FileSystemEvents();
fiEvents.ItemException += new ItemExceptionEventHandler( OnItemException );
 
DiskFolder folder = new DiskFolder( @"c:\temp" );
folder.CopyFilesTo( fiEvents, null, new DiskFolder( @"c:\destination" ), true, false );
           
//This method will handle the ItemException events that are raised.
public static void OnItemException( object sender, ItemExceptionEventArgs e )
{
  if( e. Exception is ItemAlreadyExistsException )
  {
    e.TargetItem.Delete();
    e.Action = ItemExceptionAction.Retry;
  }
}
 
//If you no longer wish to handle the events that are raised,
//you can unsubscribe from the event notifications by doing the following:
 
fiEvents.ItemException -= new ItemExceptionEventHandler( OnItemException );