Beispiel #1
0
        public async Task <IFile> MoveAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
                                            CancellationToken cancellationToken             = new CancellationToken())
        {
            var newFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(newPath))
                            .AsTask(cancellationToken)
                            .ConfigureAwait(false);

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

            try
            {
                await _storageFile.MoveAsync(newFolder, newName, collisionOption.ToNameCollisionOption())
                .AsTask(cancellationToken)
                .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                if (ex.HResult == FileAlreadyExists)
                {
                    throw new UnifiedIOException(string.Format("The file {0} already exists", newPath), ex);
                }

                throw new UnifiedIOException(
                          string.Format("Could not move the file {0} to {1}: {2}", _path, newPath, ex.Message), ex);
            }

            _path = _storageFile.Path;
            return(this);
        }
Beispiel #2
0
        /// <summary>
        /// Moves the file to the specified directory path
        /// </summary>
        /// <param name="destinationDirectoryPath"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public Task <bool> MoveAsync(string destinationDirectoryPath, CollisionOption option)
        {
            AssertFileExists();

            return(Task.Factory.StartNew(() =>
            {
                bool wasMoved = false;
                string fullDestinationPath = Environment.ExpandEnvironmentVariables(Path.Combine(destinationDirectoryPath, this.Filename));

                switch (option)
                {
                case CollisionOption.ReplaceExisting:
                    if (File.Exists(fullDestinationPath))
                    {
                        File.Delete(fullDestinationPath);
                    }
                    _fileInfo.MoveTo(fullDestinationPath);
                    wasMoved = true;
                    break;

                case CollisionOption.ThrowExisting:
                    if (File.Exists(fullDestinationPath))
                    {
                        throw new Exceptions.FileAlreadyExistsException("File already exists in the target directory");
                    }
                    goto case CollisionOption.ReplaceExisting;

                default:
                    goto case CollisionOption.ReplaceExisting;
                }

                return wasMoved;
            }));
        }
        public async Task <IFile> CreateFileAsync(string desiredName, CollisionOption option,
                                                  CancellationToken cancellationToken = new CancellationToken())
        {
            await EnsureExistsAsync(cancellationToken)
            .ConfigureAwait(false);

            StorageFile file;

            try
            {
                file = await _storage.CreateFileAsync(desiredName, option.ToCreationCollisionOption())
                       .AsTask(cancellationToken)
                       .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                if (ex.HResult == WindowsStorageFile.FileAlreadyExists)
                {
                    //  File already exists (and potentially other failures, not sure what the HResult represents)
                    throw new UnifiedIOException(ex.Message, ex);
                }
                throw;
            }

            return(new WindowsStorageFile(file));
        }
        /// <summary>
        /// Moves the directory to the destination directory specified
        /// </summary>
        /// <param name="destinationDirectoryPath"></param>
        /// <param name="collisionOption"></param>
        /// <returns></returns>
        public Task <bool> MoveAsync(string destinationDirectoryPath, CollisionOption collisionOption)
        {
            AssertDirectoryExists();

            return(Task.Factory.StartNew(() =>
            {
                bool wasMoved = false;

                switch (collisionOption)
                {
                case CollisionOption.ReplaceExisting:
                    string fullDestination = Path.Combine(_systemPathMapper.GetUserContextFolder(destinationDirectoryPath), this.Name);
                    if (Directory.Exists(fullDestination))
                    {
                        Directory.Delete(fullDestination);
                    }
                    Directory.Move(this.FullPath, fullDestination);
                    _directoryInfo = new DirectoryInfo(fullDestination);
                    wasMoved = true;
                    break;

                case CollisionOption.ThrowExisting:
                    throw new Exceptions.DirectoryAlreadyExistsException("Directory already exists in the target directory");

                default:
                    goto case CollisionOption.ReplaceExisting;
                }

                return wasMoved;
            }));
        }
        public async Task<IFile> CreateFileAsync(string desiredName, CollisionOption option,
            CancellationToken cancellationToken = new CancellationToken())
        {
            await EnsureExistsAsync(cancellationToken)
                .ConfigureAwait(false);

            StorageFile file;
            try
            {
                file = await _storage.CreateFileAsync(desiredName, option.ToCreationCollisionOption())
                    .AsTask(cancellationToken)
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                if (ex.HResult == WindowsStorageFile.FileAlreadyExists)
                {
                    //  File already exists (and potentially other failures, not sure what the HResult represents)
                    throw new UnifiedIOException(ex.Message, ex);
                }
                throw;
            }

            return new WindowsStorageFile(file);
        }
Beispiel #6
0
        public async Task <IDirectory> CreateDirectoryAsync(string desiredName, CollisionOption option,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            EnsureExists();

            string nameToUse = desiredName;
            string newPath   = System.IO.Path.Combine(Path, nameToUse);

            if (Directory.Exists(newPath))
            {
                switch (option)
                {
                case CollisionOption.GenerateUniqueName:
                {
                    for (int num = 2; Directory.Exists(newPath); num++)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        nameToUse = string.Format("{0} ({1})", desiredName, num);
                        newPath   = System.IO.Path.Combine(Path, nameToUse);
                    }

                    Directory.CreateDirectory(newPath);
                    break;
                }

                case CollisionOption.ReplaceExisting:
                {
                    Directory.Delete(newPath, true);
                    Directory.CreateDirectory(newPath);
                    break;
                }

                case CollisionOption.FailIfExists:
                    throw new UnifiedIOException(string.Format("The directory {0} already exists",
                                                               newPath));

                case CollisionOption.OpenIfExists:
                {
                    // Do nothing...
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException("option", option, null);
                }
            }
            else
            {
                Directory.CreateDirectory(newPath);
            }

            return(new DotNetDirectory(newPath));
        }
Beispiel #7
0
        public async Task <IFile> CreateFileAsync(string desiredName, CollisionOption option,
                                                  CancellationToken cancellationToken = default(CancellationToken))
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            string nameToUse = desiredName;
            string newPath   = System.IO.Path.Combine(Path, nameToUse);

            if (File.Exists(newPath))
            {
                switch (option)
                {
                case CollisionOption.GenerateUniqueName:
                {
                    string desiredRoot      = System.IO.Path.GetFileNameWithoutExtension(desiredName);
                    string desiredExtension = System.IO.Path.GetExtension(desiredName);
                    for (int num = 1; File.Exists(newPath); num++)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        nameToUse = string.Format("{0} ({1}).{2}", desiredRoot, num, desiredExtension);
                        newPath   = System.IO.Path.Combine(Path, nameToUse);
                    }

                    break;
                }

                case CollisionOption.ReplaceExisting:
                {
                    File.Delete(newPath);
                    CreateFile(newPath);

                    break;
                }

                case CollisionOption.FailIfExists:
                    throw new UnifiedIOException("Cannot create file, it already exists");

                case CollisionOption.OpenIfExists:
                {
                    // Do nothing
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException("option", option, null);
                }
            }
            else
            {
                //	Create file
                CreateFile(newPath);
            }

            return(new DotNetFile(newPath));
        }
Beispiel #8
0
        public async Task RenameAsync(string newFileName, CollisionOption collisionOption = CollisionOption.FailIfExists, CancellationToken cancellationToken = default(CancellationToken))
        {
            await Task.Run(() =>
            {
                var currentFileName = System.IO.Path.GetFileName(Path);
                var newFilePath     = Path.Replace(currentFileName, newFileName);

                System.IO.File.Move(Path, newFilePath);
                Path = newFilePath;
            }, cancellationToken);
        }
Beispiel #9
0
        public async Task <IFile> RenameAsync(string newName, CollisionOption collisionOption = CollisionOption.FailIfExists,
                                              CancellationToken cancellationToken             = default(CancellationToken))
        {
            var directory = System.IO.Path.GetDirectoryName(_path);

            if (newName.StartsWith(directory))
            {
                throw new ArgumentException("The filename must not contain a path", "newName");
            }

            return(await MoveAsync(System.IO.Path.Combine(directory, newName), collisionOption, cancellationToken));
        }
        public async Task<IFile> CreateFileAsync(string desiredName, CollisionOption option,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            string nameToUse = desiredName;
            string newPath = System.IO.Path.Combine(Path, nameToUse);

            if (File.Exists(newPath))
            {
                switch (option)
                {
                    case CollisionOption.GenerateUniqueName:
                    {
                        string desiredRoot = System.IO.Path.GetFileNameWithoutExtension(desiredName);
                        string desiredExtension = System.IO.Path.GetExtension(desiredName);
                        for (int num = 1; File.Exists(newPath); num++)
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            nameToUse = string.Format("{0} ({1}).{2}", desiredRoot, num, desiredExtension);
                            newPath = System.IO.Path.Combine(Path, nameToUse);
                        }

                        break;
                    }
                    case CollisionOption.ReplaceExisting:
                    {
                        File.Delete(newPath);
                        CreateFile(newPath);

                        break;
                    }
                    case CollisionOption.FailIfExists:
                        throw new UnifiedIOException("Cannot create file, it already exists");

                    case CollisionOption.OpenIfExists:
                    {
                        // Do nothing
                        break;
                    }

                    default:
                        throw new ArgumentOutOfRangeException("option", option, null);
                }
            }
            else
            {
                //	Create file
                CreateFile(newPath);
            }

            return new DotNetFile(newPath);
        }
        public Task<IFile> RenameAsync(string newName, CollisionOption collisionOption = CollisionOption.FailIfExists,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Make sure the source file exists
            EnsureExists();

            var directoryPath = System.IO.Path.GetDirectoryName(_path);
            if (directoryPath == null)
            {
                throw new ArgumentException("newName");
            }

            return MoveAsync( System.IO.Path.Combine(directoryPath, newName), collisionOption, cancellationToken );
        }
Beispiel #12
0
        public Task <IFile> RenameAsync(string newName, CollisionOption collisionOption = CollisionOption.FailIfExists,
                                        CancellationToken cancellationToken             = default(CancellationToken))
        {
            // Make sure the source file exists
            EnsureExists();

            var directoryPath = System.IO.Path.GetDirectoryName(_path);

            if (directoryPath == null)
            {
                throw new ArgumentException("newName");
            }

            return(MoveAsync(System.IO.Path.Combine(directoryPath, newName), collisionOption, cancellationToken));
        }
        /// <summary>
        /// Copies the contents of this directory to the target destination
        /// CAUTION: If you want to directly copy this directory you must specify this directory name in the destination path
        /// Example: CopyAsync(C:\ProgramData, CollisionOption) will just copy the files and subdirectories
        /// in the directory to ProgramData. CopyAsync(C:\ProgramData\[NameOfDirectory], CollisionOption) will be like copying
        /// the folder itself
        /// </summary>
        /// <returns></returns>
        public Task <bool> CopyAsync(string destinationPath, CollisionOption collisionOption)
        {
            AssertDirectoryExists();

            return(Task.Factory.StartNew(() =>
            {
                bool wasCopied = false;

                // Expand the destination directory and add this directory name to the target directory
                string expandedDestination = _systemPathMapper.GetUserContextFolder(destinationPath);

                // Call the copy directory function, requires new method due to recursion
                wasCopied = CopyDirectory(_directoryInfo.FullName, expandedDestination, collisionOption);

                return wasCopied;
            }));
        }
Beispiel #14
0
        public static NameCollisionOption ToNameCollisionOption(this CollisionOption option)
        {
            switch (option)
            {
            case CollisionOption.GenerateUniqueName:
                return(NameCollisionOption.GenerateUniqueName);

            case CollisionOption.ReplaceExisting:
                return(NameCollisionOption.ReplaceExisting);

            case CollisionOption.FailIfExists:
                return(NameCollisionOption.FailIfExists);

            default:
                throw new ArgumentOutOfRangeException("option", option, null);
            }
        }
Beispiel #15
0
        public async Task <ICrossFolder> CreateFolderAsync(string folderName, CollisionOption collisionOption = CollisionOption.FailIfExists, CancellationToken cancellationToken = default(CancellationToken))
        {
            return(await Task.Run(() =>
            {
                var newFolderPath = System.IO.Path.Combine(Path, folderName);
                var exists = Directory.Exists(newFolderPath);

                if (exists && collisionOption == CollisionOption.FailIfExists)
                {
                    return null;
                }
                if (exists && collisionOption == CollisionOption.ReplaceExisting)
                {
                    Directory.Delete(newFolderPath, false);
                }

                Directory.CreateDirectory(newFolderPath);

                return new CrossFolder(folderName, newFolderPath);
            }, cancellationToken));
        }
Beispiel #16
0
        /// <summary>
        /// Moves the directory to the destination directory specified
        /// </summary>
        /// <param name="destinationDirectoryPath"></param>
        /// <param name="collisionOption"></param>
        /// <returns></returns>
        public Task <bool> MoveAsync(string destinationDirectoryPath, CollisionOption collisionOption)
        {
            AssertDirectoryExists();

            return(Task.Factory.StartNew(() =>
            {
                bool wasMoved = false;

                string fullDestination = Environment.ExpandEnvironmentVariables(Path.Combine(destinationDirectoryPath, this.Name));

                if (Directory.Exists(fullDestination))
                {
                    switch (collisionOption)
                    {
                    case CollisionOption.ReplaceExisting:
                        Directory.Delete(fullDestination, true);
                        Directory.Move(this.FullPath, fullDestination);
                        _directoryInfo = new DirectoryInfo(fullDestination);
                        wasMoved = true;
                        break;

                    case CollisionOption.ThrowExisting:
                        throw new Exceptions.DirectoryAlreadyExistsException("Directory already exists in the target directory");

                    default:
                        goto case CollisionOption.ReplaceExisting;
                    }
                }
                else
                {
                    Directory.Move(this.FullPath, fullDestination);
                    _directoryInfo = new DirectoryInfo(fullDestination);
                    wasMoved = true;
                }

                return wasMoved;
            }));
        }
Beispiel #17
0
        public async Task <IFile> CopyAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
                                            CancellationToken cancellationToken             = new CancellationToken())
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            await EnsureExistsAsync(cancellationToken);

            StorageFile newFile;

            using (Stream source = await _storageFile.OpenStreamForReadAsync())
            {
                newFile = await StorageFile.GetFileFromPathAsync(newPath)
                          .AsTask(cancellationToken)
                          .ConfigureAwait(false);

                const int bufferSize = 4096;
                using (Stream destination = await newFile.OpenStreamForWriteAsync())
                {
                    await source.CopyToAsync(destination, bufferSize, cancellationToken);
                }
            }

            return(new WindowsStorageFile(newFile));
        }
Beispiel #18
0
        public async Task <IFile> CopyAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
                                            CancellationToken cancellationToken             = default(CancellationToken))
        {
            // Make sure the source file exists
            EnsureExists();

            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            using (Stream source = File.Open(_path, FileMode.Open))
            {
                const int bufferSize = 4096;

#if __UNIFIED__ || _ANDROID_
                using (Stream destination = File.Create(newPath, bufferSize))
#else
                using (Stream destination = File.Create(newPath, bufferSize, FileOptions.Asynchronous))
#endif
                {
                    await source.CopyToAsync(destination, bufferSize, cancellationToken);
                }
            }

            return(new DotNetFile(newPath));
        }
Beispiel #19
0
 public async Task MoveAsync(string newFilePath, CollisionOption collisionOption = CollisionOption.ReplaceExisting, CancellationToken cancellationToken = default(CancellationToken))
 {
     await Task.Run(() => { System.IO.File.Move(Path, newFilePath); }, cancellationToken);
 }
Beispiel #20
0
        public async Task <IFile> MoveAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
                                            CancellationToken cancellationToken             = default(CancellationToken))
        {
            // Make sure the source file exists
            EnsureExists();

            await AwaitExtensions.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 = newName;
                if (counter > 1)
                {
                    candidateName = string.Format(
                        CultureInfo.InvariantCulture,
                        "{0} ({1}){2}",
                        System.IO.Path.GetFileNameWithoutExtension(newName),
                        counter,
                        System.IO.Path.GetExtension(newName));
                }

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

                if (File.Exists(candidatePath))
                {
                    switch (collisionOption)
                    {
                    case CollisionOption.FailIfExists:
                    {
                        throw new UnifiedIOException("File already exists.");
                    }

                    case CollisionOption.GenerateUniqueName:
                    {
                        // Continue with the loop and generate a new name
                        continue;
                    }

                    case CollisionOption.ReplaceExisting:
                    {
                        File.Delete(candidatePath);
                        break;
                    }

                    default:
                    {
                        throw new ArgumentOutOfRangeException("collisionOption", collisionOption, null);
                    }
                    }
                }

                File.Move(_path, candidatePath);

                _path = candidatePath;
                return(this);
            }
        }
        public async Task<IDirectory> CreateDirectoryAsync(string desiredName, CollisionOption option,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            EnsureExists();

            string nameToUse = desiredName;
            string newPath = System.IO.Path.Combine(Path, nameToUse);
            if (Directory.Exists(newPath))
            {
                switch (option)
                {
                    case CollisionOption.GenerateUniqueName:
                    {
                        for (int num = 2; Directory.Exists(newPath); num++)
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            nameToUse = string.Format("{0} ({1})", desiredName, num);
                            newPath = System.IO.Path.Combine(Path, nameToUse);
                        }

                        Directory.CreateDirectory(newPath);
                        break;
                    }

                    case CollisionOption.ReplaceExisting:
                    {
                        Directory.Delete(newPath, true);
                        Directory.CreateDirectory(newPath);
                        break;
                    }

                    case CollisionOption.FailIfExists:
                        throw new UnifiedIOException(string.Format("The directory {0} already exists",
                            newPath));

                    case CollisionOption.OpenIfExists:
                    {
                        // Do nothing...
                        break;
                    }
                    default:
                        throw new ArgumentOutOfRangeException("option", option, null);
                }
            }
            else
            {
                Directory.CreateDirectory(newPath);
            }

            return new DotNetDirectory(newPath);
        }
        public async Task<IFile> RenameAsync(string newName, CollisionOption collisionOption = CollisionOption.FailIfExists,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var directory = System.IO.Path.GetDirectoryName(_path);
            if (newName.StartsWith(directory))
            {
                throw new ArgumentException("The filename must not contain a path", "newName");
            }

            return await MoveAsync(System.IO.Path.Combine(directory, newName), collisionOption, cancellationToken);
        }
Beispiel #23
0
        public async Task <ICrossFile> WriteTextFileAsync(string fileName, string content, CollisionOption collisionOption = CollisionOption.ReplaceExisting, CancellationToken cancellationToken = default(CancellationToken))
        {
            return(await Task.Run(() =>
            {
                var fileExtension = "txt";
                var newFilePath = System.IO.Path.Combine(Path, $"{fileName}.{fileExtension}");
                var exists = System.IO.File.Exists(newFilePath);

                if (exists && collisionOption == CollisionOption.FailIfExists)
                {
                    return null;
                }
                if (exists && collisionOption == CollisionOption.ReplaceExisting)
                {
                    System.IO.File.Delete(newFilePath);
                }

                System.IO.File.WriteAllText(newFilePath, content, Encoding.UTF8);

                return new CrossFile(fileName, fileExtension, newFilePath);
            }, cancellationToken));
        }
Beispiel #24
0
        public async Task <ICrossFile> CreateFileAsync(string fileName, string fileExtension, byte[] dataBytes, CollisionOption collisionOption = CollisionOption.FailIfExists, CancellationToken cancellationToken = default(CancellationToken))
        {
            return(await Task.Run(() =>
            {
                var newFilePath = System.IO.Path.Combine(Path, $"{fileName}.{fileExtension}");
                var exists = System.IO.File.Exists(newFilePath);

                if (exists && collisionOption == CollisionOption.FailIfExists)
                {
                    return null;
                }
                if (exists && collisionOption == CollisionOption.ReplaceExisting)
                {
                    System.IO.File.Delete(newFilePath);
                }

                System.IO.File.WriteAllBytes(newFilePath, dataBytes);

                return new CrossFile(fileName, fileExtension, newFilePath);
            }, cancellationToken));
        }
        /// <summary>
        /// Recursive function to copy directories
        /// </summary>
        private bool CopyDirectory(string source, string destination, CollisionOption collisionOption)
        {
            bool wasCopied = false;

            // Get the source directory
            DirectoryInfo sourceDir = new DirectoryInfo(source);

            // Create the directory if it does not already exist
            if (!Directory.Exists(destination))
            {
                Directory.CreateDirectory(destination);
            }

            // Get the files & sub directories for the source directory
            var files          = sourceDir.GetFiles();
            var subDirectories = sourceDir.GetDirectories();

            switch (collisionOption)
            {
            // Replaces any existing folders or files
            case CollisionOption.ReplaceExisting:
                // Iterate through each file in the directory
                foreach (var file in files)
                {
                    // Get the file info for the destination path, to see if it already exists
                    FileInfo fileDestInfo = new FileInfo(Path.Combine(destination, file.Name));
                    if (fileDestInfo.Exists)
                    {
                        // Delete the existing target file
                        fileDestInfo.Delete();
                    }

                    // Copy the file to the new destination
                    file.CopyTo(Path.Combine(destination, file.Name), true);
                }

                // Iterate through each folder in the directory
                foreach (var subDirectory in subDirectories)
                {
                    // Get the directory info for the destination path, to see if it already exists
                    DirectoryInfo dirDestInfo = new DirectoryInfo(Path.Combine(destination, subDirectory.Name));
                    if (dirDestInfo.Exists)
                    {
                        // Delete the existing target directory
                        dirDestInfo.Delete(true);
                    }

                    // Copy the directory to the new destination
                    CopyDirectory(subDirectory.FullName, dirDestInfo.FullName, collisionOption);
                }

                wasCopied = true;
                break;

            // Throws exception if there are any pre-existing files or folders
            case CollisionOption.ThrowExisting:
                // Iterate through file in the directory
                foreach (var file in files)
                {
                    // Get the file info for the destination path, to see if it already exists
                    FileInfo fileDestInfo = new FileInfo(Path.Combine(destination, file.Name));
                    if (!fileDestInfo.Exists)
                    {
                        // Copy the file to the new destination
                        file.CopyTo(fileDestInfo.FullName, true);
                    }
                    else
                    {
                        throw new Exceptions.FileAlreadyExistsException(String.Format("File path {0} already exists.", file.FullName));
                    }
                }

                // Iterate through each sub-directory
                foreach (var subDirectory in subDirectories)
                {
                    // Get the directory info for the destination path, to see if it already exists
                    DirectoryInfo dirDestInfo = new DirectoryInfo(Path.Combine(destination, subDirectory.Name));
                    if (!dirDestInfo.Exists)
                    {
                        // Copy the directory to the new destination
                        CopyDirectory(subDirectory.FullName, dirDestInfo.FullName, collisionOption);
                    }
                    else
                    {
                        throw new Exceptions.FileAlreadyExistsException(String.Format("Directory path {0} already exists.", subDirectory.FullName));
                    }
                }

                wasCopied = true;
                break;

            default:
                goto case CollisionOption.ReplaceExisting;
            }

            return(wasCopied);
        }
        public async Task<IFile> MoveAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
            CancellationToken cancellationToken = new CancellationToken())
        {
            var newFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(newPath))
                .AsTask(cancellationToken)
                .ConfigureAwait(false);

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

            try
            {
                await _storageFile.MoveAsync(newFolder, newName, collisionOption.ToNameCollisionOption())
                    .AsTask(cancellationToken)
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                if (ex.HResult == FileAlreadyExists)
                {
                    throw new UnifiedIOException(string.Format("The file {0} already exists", newPath), ex);
                }

                throw new UnifiedIOException(
                    string.Format("Could not move the file {0} to {1}: {2}", _path, newPath, ex.Message), ex);
            }

            _path = _storageFile.Path;
            return this;
        }
        public async Task<IFile> CopyAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
            CancellationToken cancellationToken = new CancellationToken())
        {
            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);
            await EnsureExistsAsync(cancellationToken);

            StorageFile newFile;

            using (Stream source = await _storageFile.OpenStreamForReadAsync())
            {
                newFile = await StorageFile.GetFileFromPathAsync(newPath)
                    .AsTask(cancellationToken)
                    .ConfigureAwait(false);

                const int bufferSize = 4096;
                using (Stream destination = await newFile.OpenStreamForWriteAsync())
                {
                    await source.CopyToAsync(destination, bufferSize, cancellationToken);
                }
            }

            return new WindowsStorageFile(newFile);
        }
        public async Task<IFile> MoveAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Make sure the source file exists
            EnsureExists();

            await AwaitExtensions.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 = newName;
                if (counter > 1)
                {
                    candidateName = string.Format(
                        CultureInfo.InvariantCulture,
                        "{0} ({1}){2}",
                        System.IO.Path.GetFileNameWithoutExtension(newName),
                        counter,
                        System.IO.Path.GetExtension(newName));
                }

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

                if (File.Exists(candidatePath))
                {
                    switch (collisionOption)
                    {
                        case CollisionOption.FailIfExists:
                        {
                            throw new UnifiedIOException("File already exists.");
                        }

                        case CollisionOption.GenerateUniqueName:
                        {
                            // Continue with the loop and generate a new name
                            continue;
                        }

                        case CollisionOption.ReplaceExisting:
                        {
                            File.Delete(candidatePath);
                            break;
                        }
                        default:
                        {
                            throw new ArgumentOutOfRangeException("collisionOption", collisionOption, null);
                        }
                    }
                }

                File.Move(_path, candidatePath);

                _path = candidatePath;
                return this;
            }
        }
        public async Task<IFile> CopyAsync(string newPath, CollisionOption collisionOption = CollisionOption.ReplaceExisting,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Make sure the source file exists
            EnsureExists();

            await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);

            using (Stream source = File.Open(_path, FileMode.Open))
            {
                const int bufferSize = 4096;

#if __UNIFIED__ || _ANDROID_
                using (Stream destination = File.Create(newPath, bufferSize))
#else
                using (Stream destination = File.Create(newPath, bufferSize, FileOptions.Asynchronous))
#endif
                {
                    await source.CopyToAsync(destination, bufferSize, cancellationToken);
                }
            }

            return new DotNetFile(newPath);
        }