/// <summary> /// Deletes a file or folder placeholder in user file system. /// </summary> /// <remarks>This method failes if the file or folder in user file system is modified (not in sync with the remote storage).</remarks> public async Task DeleteAsync() { // Windows does not provide a function to delete a placeholder file only if it is not modified. // Here we check that the file is not modified in user file system, using GetInSync() call. // To avoid the file modification between GetInSync() call and Delete() call in this method we // open it without FileShare.Write flag. using (WindowsFileSystemItem userFileSystemWinItem = WindowsFileSystemItem.Open(userFileSystemPath, (FileAccess)0, FileMode.Open, FileShare.Read | FileShare.Delete)) { if (PlaceholderItem.GetInSync(userFileSystemWinItem.SafeHandle)) { if (FsPath.IsFile(userFileSystemPath)) { File.Delete(userFileSystemPath); } else { Directory.Delete(userFileSystemPath, true); } } else { throw new IOException("File not in-sync."); } } }
//$> /// <summary> /// Deletes a file or folder placeholder in user file system. /// </summary> /// <remarks> /// This method throws <see cref="ConflictException"/> if the file or folder or any file or folder /// in the folder hierarchy being deleted in user file system is modified (not in sync with the remote storage). /// </remarks> /// <returns>True if the file was deleted. False - otherwise.</returns> public async Task <bool> DeleteAsync() { // Cloud Filter API does not provide a function to delete a placeholder file only if it is not modified. // Here we check that the file is not modified in user file system, using GetInSync() call. // To avoid the file modification between GetInSync() call and Delete() call we // open it without FileShare.Write flag. try { // Because of the on-demand population the file or folder placeholder may not exist in the user file system. if (FsPath.Exists(userFileSystemPath)) { using (WindowsFileSystemItem userFileSystemWinItem = WindowsFileSystemItem.Open(userFileSystemPath, (FileAccess)0, FileMode.Open, FileShare.Read | FileShare.Delete)) { if (PlaceholderItem.GetInSync(userFileSystemWinItem.SafeHandle)) { if (FsPath.IsFile(userFileSystemPath)) { File.Delete(userFileSystemPath); } else { Directory.Delete(userFileSystemPath, true); } // Delete ETag ETag.DeleteETag(userFileSystemPath); return(true); } else { throw new ConflictException(Modified.Client, "The item is not in-sync with the cloud."); } } } } catch (Exception ex) { await SetDownloadErrorStateAsync(ex); // Rethrow the exception preserving stack trace of the original exception. System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw(); } return(false); }