How to copy a folder in a Windows Store App

放肆的年华 提交于 2019-12-05 21:28:26
Tristan

This is rough and not thoroughly tested. It copies folders recursively. For name collisions, it overwrites existing files and folders.

    public static async Task CopyAsync(
        StorageFolder source, 
        StorageFolder destination)
    {
        // If the destination exists, delete it.
        var targetFolder = await destination.TryGetItemAsync(source.DisplayName);

        if (targetFolder is StorageFolder)
            await targetFolder.DeleteAsync();

        targetFolder = await destination.CreateFolderAsync(source.DisplayName);

        // Get all files (shallow) from source
        var queryOptions = new QueryOptions
        {
            IndexerOption = IndexerOption.DoNotUseIndexer,  // Avoid problems cause by out of sync indexer
            FolderDepth = FolderDepth.Shallow,
        };
        var queryFiles = source.CreateFileQueryWithOptions(queryOptions);
        var files = await queryFiles.GetFilesAsync();

        // Copy files into target folder
        foreach (var storageFile in files)
        {
            await storageFile.CopyAsync((StorageFolder)targetFolder, storageFile.Name, NameCollisionOption.ReplaceExisting);
        }

        // Get all folders (shallow) from source
        var queryFolders = source.CreateFolderQueryWithOptions(queryOptions);
        var folders = await queryFolders.GetFoldersAsync();

        // For each folder call CopyAsync with new destination as destination
        foreach (var storageFolder in folders)
        {
            await CopyAsync(storageFolder, (StorageFolder)targetFolder);
        }
    }

Please, someone have a better answer. Copying a folder should be a one line call to a tested .net API. We shouldn't all have to write our own functions or copy-paste untested code from the internet.

Here is my version for copying folders, if has 3 extension methods on IStorageFolder:

  1. shallow copy of all files and folders
  2. recursive copy of all files and folders
  3. copy just files

Code:

public static class StorageFolderExtensions
{
    /// <summary>
    /// Recursive copy of files and folders from source to destination.
    /// </summary>
    public static async Task CopyContentsRecursive(this IStorageFolder source, IStorageFolder dest)
    {
        await CopyContentsShallow(source, dest);

        var subfolders = await source.GetFoldersAsync();
        foreach (var storageFolder in subfolders)
        {
            await storageFolder.CopyContentsRecursive(await dest.GetFolderAsync(storageFolder.Name));
        }
    }

    /// <summary>
    /// Shallow copy of files and folders from source to destination.
    /// </summary>
    public static async Task CopyContentsShallow(this IStorageFolder source, IStorageFolder destination)
    {
        await source.CopyFiles(destination);

        var items = await source.GetFoldersAsync();

        foreach (var storageFolder in items)
        {
            await destination.CreateFolderAsync(storageFolder.Name, CreationCollisionOption.ReplaceExisting);
        }
    }

    /// <summary>
    /// Copy files from source into destination folder.
    /// </summary>
    private static async Task CopyFiles(this IStorageFolder source, IStorageFolder destination)
    {
        var items = await source.GetFilesAsync();

        foreach (var storageFile in items)
        {
            await storageFile.CopyAsync(destination, storageFile.Name, NameCollisionOption.ReplaceExisting);
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!