I have SSD storage and this code takes 32 seconds to move ~200 files and ~40 folders to the same storage in either debug or release mode. total size of folder is ~30 MB.
How can i make this faster?
// moves content from local folder to target folder.
async Task MoveContent(IStorageFolder source, IStorageFolder destination)
{
foreach(var item in await source.GetItemsAsync())
{
switch (item)
{
case IStorageFile sourceFile:
await sourceFile.MoveAsync(destination, sourceFile.Name, NameCollisionOption.ReplaceExisting);
break;
case IStorageFolder sourceSubFolder:
var destinationSubFolder = await destination.CreateFolderAsync(sourceSubFolder.Name, CreationCollisionOption.ReplaceExisting);
await MoveContent(sourceSubFolder, destinationSubFolder);
break;
}
}
}
And I call it like this
await MoveContent(extractionFolder, targetFolder);
Note that extractionFolder is in ApplicationData.Current.LocalCacheFolder and targetFolder is any folder chosen by user via FolderPicker
To improve the performance of your code, you could try enumerating all files in the folder and subfolders at once instead of throughout the folder structure (folder by folder):
Where
storageFolderis the folder you want to move. The custom file query hasFolderDepthsetting set toDeepso that it returns all files from the whole folder structure. After running this,filesarray will contain all the files and you can then move them. This will be at least a tad faster than enumerating all folders one by one. You just have to make sure to always check the appropriate subfolders are created in the target location.Finally you could try to parallelize the move
Tasks- for example moving three files at once. You can create multipleTaskinstances andawaitthem all usingTask.WhenAll.Copy-paste solution
Another quick and dirty solution would be to use
StorageFolder.CopyAsync()method to copy the folder to the new location and delete the original (this is even suggested in Docs):However, the cost of additional storage space is not very appealing and may not even improve the performance, because copying is more costly than moving.