For now, I just know how to copy file using:

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

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

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

When I try to copy folder and all the content inside, I cannot find the API like CopyAsync above.

Is it possible to copy folder and all the content in WinRT?

有帮助吗?

解决方案

Code above didn't satisfy me (too specific), i made my own generic one so figured i could share it :

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

其他提示

One possibility would be to use

StorageFolder.GetItemsAsync();

from Windows.Storage-namespace.

The result of the call is a

IReadOnlyList<IStorageItem>

containing all of the files and folders of the current folder. You can then work on those.

See MSDN for a further reference.

Base on bash.d answer, I generate my own Copy Folder:

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

This example using root as the root folder:

- 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

It will copy from InstalledLocation folder to LocalFolder.

Not sure if C# supports the Array.map method, but this is what the copyFolderAsync code would look like for 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);
      }));
    });
  });
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top