Ejemplo n.º 1
0
        /// <summary>
        /// Gets a folder, given its path.  Returns null if the folder does not exist.
        /// </summary>
        /// <param name="path">The path to a folder, as returned from the <see cref="IFolder.Path"/> property.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A folder for the specified path, or null if it does not exist.</returns>
        public async Task <IFolder> GetFolderFromPathAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNullOrEmpty(path, nameof(path));

            await AsynchronityUtilities.SwitchOffMainThreadAsync(cancellationToken);

            return(Directory.Exists(path) ? new Folder(path, true) : null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a file, given its path.  Returns null if the file does not exist.
        /// </summary>
        /// <param name="path">The path to a file, as returned from the <see cref="IFile.Path"/> property.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A file for the given path, or null if it does not exist.</returns>
        public async Task <IFile> GetFileFromPathAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNullOrEmpty(path, nameof(path));

            await AsynchronityUtilities.SwitchOffMainThreadAsync(cancellationToken);

            return(System.IO.File.Exists(path) ? new File(path) : null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
        /// <returns>A task which will complete after the file is deleted.</returns>
        public async Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            await AsynchronityUtilities.SwitchOffMainThreadAsync(cancellationToken);

            if (!System.IO.File.Exists(Path))
            {
                throw new System.IO.FileNotFoundException("File does not exist: " + Path);
            }

            System.IO.File.Delete(Path);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Opens the file.
        /// </summary>
        /// <param name="fileAccess">Specifies whether the file should be opened in read-only or read/write mode.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A <see cref="Stream"/> which can be used to read from or write to the file.</returns>
        public async Task <Stream> OpenAsync(FileAccess fileAccess, CancellationToken cancellationToken = default(CancellationToken))
        {
            await AsynchronityUtilities.SwitchOffMainThreadAsync(cancellationToken);

            switch (fileAccess)
            {
            case FileAccess.Read:
                return(System.IO.File.OpenRead(Path));

            case FileAccess.ReadWrite:
                return(System.IO.File.Open(Path, FileMode.Open, FileAccess.ReadWrite));
            }
            throw new ArgumentException("Unrecognized FileAccess value: " + fileAccess);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Moves a file.
        /// </summary>
        /// <param name="newPath">The new full path of the file.</param>
        /// <param name="collisionOption">How to deal with collisions with existing files.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A task which will complete after the file is moved.</returns>
        public async Task MoveAsync(string newPath, NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNullOrEmpty(newPath, nameof(newPath));

            await AsynchronityUtilities.SwitchOffMainThreadAsync(cancellationToken);

            string newDirectory = System.IO.Path.GetDirectoryName(newPath);
            string newName      = System.IO.Path.GetFileName(newPath);

            for (int counter = 1; ; counter++)
            {
                cancellationToken.ThrowIfCancellationRequested();
                string candidateName = counter > 1 ? $"{System.IO.Path.GetFileNameWithoutExtension(newName)} ({counter}){System.IO.Path.GetExtension(newName)}" : newName;

                string candidatePath = System.IO.Path.Combine(newDirectory, candidateName);

                if (System.IO.File.Exists(candidatePath))
                {
                    switch (collisionOption)
                    {
                    case NameCollisionOption.FailIfExists:
                        throw new IOException("File already exists.");

                    case NameCollisionOption.GenerateUniqueName:
                        continue;     // try again with a new name.

                    case NameCollisionOption.ReplaceExisting:
                        System.IO.File.Delete(candidatePath);
                        break;
                    }
                }

                System.IO.File.Move(Path, Path = candidatePath);
                Name = candidateName;
                return;
            }
        }