7

现在,我只知道如何使用以下方法复制文件:

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

4 回答 4

11

上面的代码没有让我满意(太具体了),我做了我自己的通用代码,所以我想我可以分享它:

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);
        }
    }
于 2015-01-06T11:27:43.577 回答
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.

于 2013-08-15T11:00:17.380 回答
1

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

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);
      }));
    });
  });
};
于 2014-06-11T11:56:39.950 回答
1

一种可能性是使用

StorageFolder.GetItemsAsync();

来自Windows.Storage-命名空间。

调用的结果是

IReadOnlyList<IStorageItem>

包含当前文件夹的所有文件和文件夹。然后,您可以处理这些。

有关进一步的参考,请参阅MSDN

于 2013-08-15T08:12:14.100 回答