Copy folder to WinRT

Currently, I just know how to copy a 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 the folder and all the contents inside, I can not find the API like CopyAsync above.

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

+4
source share
4 answers

The code above did not satisfy me (too specific), I made my own general, so that 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); } } 
+6
source

One possibility would be to use

 StorageFolder.GetItemsAsync(); 

from Windows.Storage -namespace.

The result of the call is

 IReadOnlyList<IStorageItem> 

containing all the files and folders of the current folder. Then you can work on them.

See MSDN for more details.

+1
source

Base on bash.d answer, I create 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 uses 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 to the LocalFolder folder.

+1
source

Not sure C # supports Array.map method, but this is what copyFolderAsync JavaScript code will look like

 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); })); }); }); }; 
+1
source

Source: https://habr.com/ru/post/1497182/


All Articles