IFileSystemItemMoveToAsync Method |
Namespace: ITHit.FileSystem
Task MoveToAsync( string targetUserFileSystemPath, byte[] targetFolderRemoteStorageItemId, IOperationContext operationContext, IConfirmationResultContext resultContext, CancellationToken cancellationToken = null )
This method is called before a file or a folder is being moved to a new location or renamed.
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, the operation is confirmed automatically.
Note that this method is called only when the Engine is running. If the item is being moved when the Engine was stopped, only the [!:MoveToCompletionAsync] method is called when the Engine is started and the [!:OutgoingSync.ProcessAsync] method is called.
The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.
public async Task MoveToAsync(string targetUserFileSystemPath, byte[] targetFolderRemoteStorageItemId, IOperationContext operationContext, IConfirmationResultContext resultContext, CancellationToken cancellationToken = default) { IWindowsMoveContext moveContext = operationContext as IWindowsMoveContext; string userFileSystemNewPath = moveContext.TargetPath; string userFileSystemOldPath = this.UserFileSystemPath; Logger.LogDebug($"{nameof(IFileSystemItem)}.{nameof(MoveToAsync)}()", userFileSystemOldPath, userFileSystemNewPath, moveContext); } public async Task MoveToCompletionAsync(string targetUserFileSystemPath, byte[] targetFolderRemoteStorageItemId, IWindowsMoveContext moveContext, IInSyncStatusResultContext resultContext, CancellationToken cancellationToken = default) { string userFileSystemNewPath = moveContext.TargetPath; string userFileSystemOldPath = this.UserFileSystemPath; Logger.LogMessage($"{nameof(IFileSystemItem)}.{nameof(MoveToCompletionAsync)}()", userFileSystemOldPath, userFileSystemNewPath, moveContext); FileSystemInfo remoteStorageOldItem = FsPath.GetFileSystemItem(RemoteStoragePath); if (remoteStorageOldItem != null) { string remoteStorageNewPath = mapping.MapPath(userFileSystemNewPath); if (remoteStorageOldItem is FileInfo) { if (File.Exists(remoteStorageNewPath)) { File.Delete(remoteStorageNewPath); } (remoteStorageOldItem as FileInfo).MoveTo(remoteStorageNewPath); } else { (remoteStorageOldItem as DirectoryInfo).MoveTo(remoteStorageNewPath); } Logger.LogDebug("Moved in the remote storage successfully", userFileSystemOldPath, userFileSystemNewPath, moveContext); } }