public interface IDesktopTrash Moves files and directories to the user's trash: the freedesktop.org Trash on Linux, the Trash on macOS and the Recycle Bin on Windows, from where the user can restore them.
Remarks
A path is a fully qualified local path to a file, a directory with its contents, or a symbolic link, which is trashed itself and not followed. Paths are supplied by trusted C# application code, never directly by a web client.
Outcomes common to every platform: a moved item is Success; a path that does not exist is PlatformFailureCode.NotFound; access refused is PlatformFailureCode.PermissionDenied; a location without a usable trash, such as a network share or a volume that has none, is Unavailable with PlatformUnavailableReason.BackendUnavailable. The item is never deleted permanently instead: it stays where it was unless the result is a success. A failed input/output operation, such as on a full disk, is PlatformFailureCode.IOError; other native errors are PlatformFailureCode.Other with a diagnostic.
Listing, restoring and emptying the trash are available on Windows and on Linux outside Flatpak and Snap. macOS has no public interface for them and the Trash portal only moves items to the trash, so there they report Unavailable with PlatformUnavailableReason.BackendUnavailable, as the default implementations do.
Methods
EmptyAsync
ValueTask<PlatformResult<PlatformUnit>> EmptyAsync(CancellationToken cancellationToken = default) Deletes every item in the trash permanently, without asking the user.
Parameters
cancellationTokenCancels the request before deletion starts; a deletion in progress completes.
Remarks
Ask the user first: the items cannot be restored afterwards. The trashes IDesktopTrash.ListAsync lists are emptied; a symbolic link is deleted itself and never followed. The default implementation returns Unavailable with PlatformUnavailableReason.BackendUnavailable.
ListAsync
ValueTask<PlatformResult<ImmutableArray<DesktopTrashItem>>> ListAsync(CancellationToken cancellationToken = default) Lists the items in the user's trash.
Parameters
cancellationTokenCancels the request.
Returns
The items, in no particular order: on Windows those of the Recycle Bin of every drive, on Linux those of the home trash and of the trash directories of mounted filesystems. An empty trash is an empty array.
Remarks
The default implementation returns Unavailable with PlatformUnavailableReason.BackendUnavailable.
MoveAllToTrashAsync
ValueTask<ImmutableArray<PlatformResult<PlatformUnit>>> MoveAllToTrashAsync(IReadOnlyList<string> paths, IProgress<DesktopTrashProgress>? progress = null, CancellationToken cancellationToken = default) Moves several files, directories or symbolic links to the trash, one after another, reporting progress.
Parameters
pathsFully qualified local paths, each checked as
IDesktopTrash.MoveToTrashAsyncchecks one.progressReceives the number of items handled after each one, and the total.
cancellationTokenCancels before the next item is moved; a move in progress completes.
Returns
One result per path, in the order of paths, each as IDesktopTrash.MoveToTrashAsync reports it. A failed item does not stop the others.
Exceptions
ArgumentNullExceptionpathsisnull.ArgumentExceptionA path is empty, relative, contains NUL or names a root directory; nothing was moved.
Remarks
The default implementation calls IDesktopTrash.MoveToTrashAsync for each path, so each item is moved or left where it was on its own, and never deleted permanently. Cancellation throws OperationCanceledException after the item in progress: the items before it, as many as progress last reported, are in the trash.
MoveToTrashAsync
ValueTask<PlatformResult<PlatformUnit>> MoveToTrashAsync(string path, CancellationToken cancellationToken = default) Moves the file, directory or symbolic link at path to the trash.
Parameters
pathA fully qualified local path.
cancellationTokenCancels the request before the item is moved; a move in progress completes.
Exceptions
ArgumentExceptionpathis empty, relative, contains NUL or names a root directory.
RestoreAsync
ValueTask<PlatformResult<PlatformUnit>> RestoreAsync(string itemId, CancellationToken cancellationToken = default) Moves a trashed item back to its DesktopTrashItem.OriginalPath.
Parameters
itemIdThe item's
DesktopTrashItem.IdfromIDesktopTrash.ListAsync.cancellationTokenCancels the request before the item is moved.
Returns
Success once the item is back; PlatformFailureCode.Conflict when something now exists at the original path, which is never replaced; PlatformFailureCode.NotFound when the item is no longer in the trash. A missing parent directory of the original path is created again.
Exceptions
ArgumentExceptionitemIdis empty.
Remarks
The default implementation returns Unavailable with PlatformUnavailableReason.BackendUnavailable.