Click or drag to resize

IFileSystemItemDeleteAsync Method

IT Hit User File System
Called before a file or a folder is being deleted from the user file system.

Namespace:  ITHit.FileSystem
Assembly:  ITHit.FileSystem (in ITHit.FileSystem.dll) Version: 8.1.26727.0-Beta2
Syntax
Task DeleteAsync(
	IOperationContext operationContext,
	IConfirmationResultContext resultContext,
	CancellationToken cancellationToken = null
)

Parameters

operationContext
Type: ITHit.FileSystemIOperationContext
Provides information about the environment.
resultContext
Type: ITHit.FileSystemIConfirmationResultContext
Used to confirm the operation, report progress and status to the platform.
cancellationToken (Optional)
Type: System.ThreadingCancellationToken
The token to monitor for cancellation requests.

Return Value

Type: Task
A task object that can be awaited.
Remarks

This method is called before a file or a folder is being deleted.

Inside this method you can accept or reject the operation. To confirm the operation call the ReturnConfirmationResult method of the IConfirmationResultContext interface, passed via the resultContext parameter. To reject the operation call the ReturnErrorResult(CloudFileStatus) method or throw any exception. If no exception is thrown in this method, the operation is confirmed automatically.

Note that this method is called only when the Engine is running. If the item is being deleted when the Engine was stopped, only the [!:DeleteCompletionAsync] method is called when the Engine is started and the [!:OutgoingSync.ProcessAsync] method is called.

Examples

The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.

C#
public async Task DeleteAsync(IOperationContext operationContext, IConfirmationResultContext resultContext = null, CancellationToken cancellationToken = default)
{
    Logger.LogDebug($"{nameof(IFileSystemItem)}.{nameof(DeleteAsync)}()", this.UserFileSystemPath, default, operationContext);

    // To cancel the operation and prevent the file from being deleted, 
    // call the resultContext.ReturnErrorResult() method or throw any exception inside this method:
    // resultContext.ReturnErrorResult(CloudFileStatus.STATUS_CLOUD_FILE_REQUEST_TIMEOUT);

    // IMPOTRTANT!
    // Make sure you have all Windows updates installed.
    // See Windows Cloud API delete prevention bug description here: 
    // https://stackoverflow.com/questions/68887190/delete-in-cloud-files-api-stopped-working-on-windows-21h1
    // https://docs.microsoft.com/en-us/answers/questions/75240/bug-report-cfapi-ackdelete-borken-on-win10-2004.html
}

public async Task DeleteCompletionAsync(IOperationContext operationContext, IInSyncStatusResultContext resultContext, CancellationToken cancellationToken = default)
{
    // On Windows, for rename with overwrite to function properly for folders, 
    // the deletion of the folder in the remote storage must be done in DeleteCompletionAsync()
    // Otherwise the source folder will be deleted before files in it can be moved.

    Logger.LogMessage($"{nameof(IFileSystemItem)}.{nameof(DeleteCompletionAsync)}()", UserFileSystemPath, default, operationContext);

    try
    {
        FileSystemInfo remoteStorageItem = FsPath.GetFileSystemItem(RemoteStoragePath);
        if (remoteStorageItem != null)
        {
            if (remoteStorageItem is FileInfo)
            {
                remoteStorageItem.Delete();
            }
            else
            {
                (remoteStorageItem as DirectoryInfo).Delete(true);
            }
            Logger.LogDebug("Deleted in the remote storage successfully", UserFileSystemPath, default, operationContext);
        }
    }
    catch (UnauthorizedAccessException)
    {
        resultContext.SetInSync = false; // We want the Engine to try deleting this file again at a later time.
        Logger.LogError("Failed to delete item", UserFileSystemPath, default, null, operationContext);
    }
    catch (DirectoryNotFoundException)
    {
        // Windows Explorer may call delete more than one time on the same file/folder.
        Logger.LogDebug("Folder already deleted", UserFileSystemPath, default, operationContext);
    }
    catch (FileNotFoundException)
    {
        // Windows Explorer may call delete more than one time on the same file/folder.
        Logger.LogDebug("File already deleted", UserFileSystemPath, default, operationContext);
    }
}
See Also