Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Folder on WinRT

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?

like image 599
Crazenezz Avatar asked Aug 15 '13 07:08

Crazenezz


1 Answers

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);
        }
    }
like image 59
Jérôme S. Avatar answered Oct 19 '22 20:10

Jérôme S.