ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

CopyAsync

Method for copying a folder to a new location

Source

/// <summary>
/// Method for copying a folder to a new location
/// </summary>
/// <param name="folder">source</param>
/// <param name="target">target location</param>
/// <param name="option">What to do then there is a collision</param>
/// <returns></returns>
public static async Task CopyAsync(this StorageFolder folder, StorageFolder target, CreationCollisionOption option)
{
    var targetfolder = await target.CreateFolderAsync(folder.Name, option).AsTask().ConfigureAwait(false);
    var subfolders = await folder.GetFoldersAsync().AsTask().ConfigureAwait(false);
    foreach(var subfolder in subfolders)
    {
        await subfolder.CopyAsync(targetfolder, option).ConfigureAwait(false);
    }
    var files = await folder.GetFilesAsync().AsTask().ConfigureAwait(false);
    foreach(var file in files)
    {
        await file.CopyAsync(targetfolder).AsTask().ConfigureAwait(false);
    }
}

Example

public async void CopyDemo()
{
    var newfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("new folder");
    var newfile = await newfolder.CreateFileAsync("testfile");
    var targetfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("target");

    await newfolder.CopyAsync(targetfolder, CreationCollisionOption.ReplaceExisting);
}

Author: Dave Smits

Submitted on: 6 mei 2014

Language: C#

Type: StorageFolder

Views: 3457