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.
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. |
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.
To subscribe to the ItemException event, the following steps must be performed:
Create a reference to a FileSystemEvents object using the WithEvents keyword.
Select the ItemException event from the list of available methods in the newly instantiated FileSystemEvents object. This is done in the same manner as, for example, adding the DoubleClick event of a ListBox.
You can also subscribe to the event using the AddHandler/AddressOf statement.
Place the desired code in the newly added event handler.
This example demonstrates how to use the ItemException event to delete a target file that already exists.
VB.NET | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
using Xceed.FileSystem; |