public override async Task <bool> ExistsAsync(CancellationToken cancellationToken = default) { try { await FsHelper.GetFolderAsync(_fullPath, cancellationToken).ConfigureAwait(false); return(true); } catch (DirectoryNotFoundException) { return(false); } catch (IOException) { // IOException might be thrown if a conflicting file exists. // In such cases the specification requires us to return false. try { await FsHelper.GetFileAsync(_fullPath, cancellationToken).ConfigureAwait(false); return(false); } catch { // No conflicting file exists. Rethrow the original IOException. } throw; } }
public override async Task <StorageFileProperties> GetPropertiesAsync(CancellationToken cancellationToken = default) { var file = await FsHelper.GetFileAsync(_fullPath, cancellationToken).ConfigureAwait(false); var props = await file.GetBasicPropertiesAsync().AsAwaitable(cancellationToken); var lastModified = props.DateModified == default ? (DateTimeOffset?)null : props.DateModified; return(new StorageFileProperties( file.Name, IOPath.GetFileNameWithoutExtension(file.Name), PhysicalPathHelper.GetExtensionWithoutTrailingExtensionSeparator(file.Name)?.ToNullIfEmpty(), file.DateCreated, lastModified, props.Size )); }
public override Task SetAttributesAsync(IOFileAttributes attributes, CancellationToken cancellationToken = default) { // There's no "native" API for setting file/folder attributes. // We can try to use System.IO's API - it should at least work in certain locations // like the application data. if (!EnumInfo.IsDefined(attributes)) { throw new ArgumentException(ExceptionStrings.Enum.UndefinedValue(attributes), nameof(attributes)); } return(Task.Run(async() => { // Get the file to ensure that it exists and to throw the appropriate exception. await FsHelper.GetFileAsync(_fullPath, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); File.SetAttributes(_fullPath.ToString(), attributes); }, cancellationToken)); }
public override async Task <StorageFile> MoveAsync( StoragePath destinationPath, NameCollisionOption options, CancellationToken cancellationToken = default ) { _ = destinationPath ?? throw new ArgumentNullException(nameof(destinationPath)); if (!EnumInfo.IsDefined(options)) { throw new ArgumentException(ExceptionStrings.Enum.UndefinedValue(options), nameof(options)); } if (!(destinationPath is PhysicalStoragePath)) { throw new ArgumentException( ExceptionStrings.FsCompatibility.StoragePathTypeNotSupported(), nameof(destinationPath) ); } if (destinationPath.FullPath.Parent is null) { throw new IOException(ExceptionStrings.StorageFile.CannotMoveToRootLocation()); } var fullDestinationPath = destinationPath.FullPath; var destinationFile = FileSystem.GetFile(fullDestinationPath); var file = await FsHelper.GetFileAsync(_fullPath, cancellationToken).ConfigureAwait(false); var destFolder = await FsHelper .GetFolderAsync(fullDestinationPath.Parent, cancellationToken) .ConfigureAwait(false); await file .MoveAsync(destFolder, fullDestinationPath.Name, options.ToWinOptions()) .AsTask(cancellationToken) .WithConvertedException() .ConfigureAwait(false); return(destinationFile); }
public override async Task <StorageFile> RenameAsync( string newName, NameCollisionOption options, CancellationToken cancellationToken = default ) { _ = newName ?? throw new ArgumentNullException(nameof(newName)); if (newName.Length == 0) { throw new ArgumentException(ExceptionStrings.String.CannotBeEmpty(), nameof(newName)); } if (newName.Contains(PhysicalPathHelper.InvalidNewNameCharacters)) { throw new ArgumentException( ExceptionStrings.StorageFile.NewNameContainsInvalidChar(FileSystem.PathInformation), nameof(newName) ); } if (!EnumInfo.IsDefined(options)) { throw new ArgumentException(ExceptionStrings.Enum.UndefinedValue(options), nameof(options)); } var destinationPath = _fullParentPath.Join(newName).FullPath; var destinationFile = FileSystem.GetFile(destinationPath); var file = await FsHelper.GetFileAsync(_fullPath, cancellationToken).ConfigureAwait(false); await file .RenameAsync(newName, options.ToWinOptions()) .AsTask(cancellationToken) .WithConvertedException() .ConfigureAwait(false); return(destinationFile); }
public override async Task <IOFileAttributes> GetAttributesAsync(CancellationToken cancellationToken = default) { var file = await FsHelper.GetFileAsync(_fullPath, cancellationToken).ConfigureAwait(false); return(file.Attributes.ToIOFileAttributes()); }