在WinRT上复制文件夹

时间:2013-08-15 07:43:06

标签: c# windows-8 copy winrt-async

目前,我只知道如何使用以下方式复制文件:

IStorageFolder dir = Windows.Storage.ApplicationData.Current.LocalFolder;

IStorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
    new Uri("ms-appx:///file.txt"));

await file.CopyAsync(dir, "file.txt");

当我尝试复制文件夹及其中的所有内容时,我找不到上面的CopyAsync这样的API。

是否可以在WinRT中复制文件夹和所有内容?

4 个答案:

答案 0 :(得分:6)

上面的代码并不能让我满意(太具体了),我制作了自己的通用代码,所以我想可以分享它:

public static async Task CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer, string desiredName = null)
    {
        StorageFolder destinationFolder = null;
            destinationFolder = await destinationContainer.CreateFolderAsync(
                desiredName ?? source.Name, CreationCollisionOption.ReplaceExisting);

        foreach (var file in await source.GetFilesAsync())
        {
            await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.ReplaceExisting);
        }
        foreach (var folder in await source.GetFoldersAsync())
        {
            await CopyFolderAsync(folder, destinationFolder);
        }
    }

答案 1 :(得分:1)

一种可能性是使用

StorageFolder.GetItemsAsync();

来自Windows.Storage - 名称空间。

通话结果是

IReadOnlyList<IStorageItem>

包含当前文件夹的所有文件和文件夹。 然后你可以处理那些。

有关详细信息,请参阅MSDN

答案 2 :(得分:1)

基于bash.d回答,我生成了自己的复制文件夹:

namespace Directories
{
    private string ROOT = "root";

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            CopyFolder(ROOT);
        }

        private async void CopyFolder(string path)
        {
            IStorageFolder destination = Windows.Storage.ApplicationData.Current.LocalFolder;
            IStorageFolder root = Windows.ApplicationModel.Package.Current.InstalledLocation;

            if (path.Equals(ROOT) && !await FolderExistAsync(ROOT))
                await destination.CreateFolderAsync(ROOT);

            destination = await destination.GetFolderAsync(path);
            root = await root.GetFolderAsync(path);

            IReadOnlyList<IStorageItem> items = await root.GetItemsAsync();
            foreach (IStorageItem item in items)
            {
                if (item.GetType() == typeof(StorageFile))
                {
                    IStorageFile presFile = await StorageFile.GetFileFromApplicationUriAsync(
                        new Uri("ms-appx:///" + path.Replace("\\", "/") + "/" + item.Name));

                    // Do copy file to destination folder
                    await presFile.CopyAsync(destination);
                }
                else
                {
                    // If folder doesn't exist, than create new one on destination folder
                    if (!await FolderExistAsync(path + "\\" + item.Name))
                        await destination.CreateFolderAsync(item.Name);

                    // Do recursive copy for every items inside
                    CopyFolder(path + "\\" + item.Name);
                }
            }
        }

        private async Task<bool> FolderExistAsync(string foldername)
        {
            IStorageFolder destination = Windows.Storage.ApplicationData.Current.LocalFolder;

            try
            {
                await destination.GetFolderAsync(foldername);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

此示例使用root作为根文件夹:

- root
  - sub1
    - sub1-1.txt
    - sub1-2.txt
    - sub1-3.txt
    - sub1-4.txt
  - sub2
    - sub2-1.txt
    - sub2-2.txt
    - sub2-3.txt
    - sub2-4.txt
  - root1.txt
  - root2.txt
  - root3.txt
  - root4.txt

它会从InstalledLocation文件夹复制到LocalFolder

答案 3 :(得分:1)

不确定C#是否支持Array.map方法,但这就是copyFolderAsync代码对JavaScript的看法

CreationCollisionOption = Windows.Storage.CreationCollisionOption;
NameCollisionOption = Windows.Storage.NameCollisionOption;

copyFolderAsync = function(sourceFolder, destFolder) {
  return destFolder.createFolderAsync(sourceFolder.name, CreationCollisionOption.openIfExists).then(function(destSubFolder) {
    return sourceFolder.getFilesAsync().then(function(files) {
      return WinJS.Promise.join(files.map(function(file) {
        return file.copyAsync(destSubFolder, file.name, NameCollisionOption.replaceExisting);
      }));
    }).then(function() {
      return sourceFolder.getFoldersAsync();
    }).then(function(folders) {
      return WinJS.Promise.join(folders.map(function(folder) {
        return copyFolderAsync(folder, destSubFolder);
      }));
    });
  });
};