I am currently making an app which needs to be very effective on the file system side, since it is constantly using it.
I made my own method to move a StorageFolder to another StorageFolder since there is not any method for now.
public async Task MoveTo(StorageFolder source, StorageFolder destination)
{
destination = await source.CreateFolderAsync(source.Name, CreationCollisionOption.OpenIfExists);
foreach(StorageFile file in await source.GetFilesAsync())
{
await file.MoveAsync(destination, file.Name, NameCollisionOption.ReplaceExisting);
}
foreach(StorageFolder folder in await source.GetFoldersAsync())
{
await MoveTo(folder, destination);
}
}
Unfortunately, I figured that this was not satisfying my expectations. Due to that reason, I've decided to think of another solution, which would be the following one:
- Using the
await StorageFolder.CreateFileQueryWithOptions(options);
method withFolderDepth.Deep
so all the files can be fetched in a list. - List of
Task
in which eachTask
would move a file to the destination, but before that, the whole folder structure would be re-created from the path. - Finish up using
Task.WhenAll
to move all the files to the directory they belong to.
I've tried for hours to figure out a method to recursively create the structure directory from the file paths, but could not succeed. Here is what I've tried so far:
private async Task CreateFolderStructureFromFile(StorageFile source, StorageFolder dest)
{
// Get index of first slash
int currentPosition = 0;
while (currentPosition != -1)
{
// Get the next slash.
currentPosition = source.Path.IndexOf(@"\", currentPosition + 1);
// Using the Win32 API for creating a directory without the need of declaring a new StorageFolder.
// Of course, the app has permissions since it is using a FileOpenPicker before then.
Win32.CreateDirectoryFromApp(path.Substring(0, currentPosition);
}
}
This method seems to do nothing, the first folder is created, but the others are not. If anyone needs some additional details, please let me know so I can update my question with it!
Thank you all in advance for your help!
>` return type, and then insert ` file.MoveAsync` into list, call task when all for above returen list at last.
– Nico Zhu Jul 05 '22 at 10:51