如何复制 Windows 应用商店应用程序中的文件夹 [英] How to copy a folder in a Windows Store App

查看:53
本文介绍了如何复制 Windows 应用商店应用程序中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Windows 应用商店应用中复制文件夹及其内容?

How do you copy a folder and its contents in Windows Store Apps?

我正在为 Windows 应用商店应用编写测试.该应用程序处理文件,因此需要一组已知文件.理想情况下,任何开发人员都可以运行这些测试,而无需他们进行一些手动设置.

I'm writing tests for a Windows Store App. The app does things with files, so a set of known files is needed. Ideally, any developer could run these tests without requiring that they do some manual setup.

我认为这意味着测试文件将被检入源代码管理,然后复制到 LocalState 文件夹中,测试可以在其中使用它们(在 ClassInitialize 测试阶段复制).

I assume that means test files would be checked into source control and then copied to the LocalState folder where tests could consume them (copy during ClassInitialize test phase).

StorageFile 具有复制功能.可以使用这些来递归地重建要复制的文件夹.然而,很难相信这是正确的方法......我肯定错过了一些东西.

StorageFile has copy functions. It would be possible to to use these to recursively rebuild the folder to copy. However it's hard to believe that this would be the correct approach... surely I'm missing something.

推荐答案

这是粗略的,没有经过彻底的测试.它递归地复制文件夹.对于名称冲突,它会覆盖现有文件和文件夹.

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);
        }
    }

拜托,有人有更好的答案.复制文件夹应该是对经过测试的 .net API 的一行调用.我们不应该都必须编写自己的函数或从互联网上复制粘贴未经测试的代码.

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.

这篇关于如何复制 Windows 应用商店应用程序中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆