0

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 with FolderDepth.Deep so all the files can be fetched in a list.
  • List of Task in which each Task 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!

Osaxely
  • 66
  • 1
  • 16
  • 1
    I don't fully understand your question, but looks like you're having difficulty creating the destination folder structure from the source path / folder structure. Please see: https://stackoverflow.com/questions/2134392/how-to-create-multiple-directories-from-a-single-full-path-in-c – Jonathan Jul 04 '22 at 18:48
  • Unfortunately, Directory.CreateDirectory is not possible in UWP. And I'd like to recreate all the folders from a path. For example, if I give "C:\Users\X\Documents\folder1\folder2\file.txt", I'd like my app to create all the directories (folder1 and folder2) within the Documents folder. – Osaxely Jul 04 '22 at 20:37
  • Do you mean move the source folder to the destnation folder and keep original folder structure? – Nico Zhu Jul 05 '22 at 08:13
  • Exactly! On the destination folder, I'd like to keep the structure. But to speed up the process, I'd like to re-create the whole folder structure from the full path provided by the ``StorageFile``, if you get what I mean. I'm sorry if that isn't too clear. – Osaxely Jul 05 '22 at 08:16
  • 1
    I think you can edit base on the fist MoveTo method that change it as `Task>` 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
  • That'd work. But I also want the directory structure to be created on the destination, from the file path. If the file path is ``C:\Users\Osaxely\Documents\Folder\Folder1``, it should create the two folders recursively if they don't exist. – Osaxely Jul 05 '22 at 12:07
  • 1
    It's hard to implement to create folder with path, you may need make desktop extension for your app and process it with general desktop api. – Nico Zhu Jul 06 '22 at 01:02
  • Could be. I'll do my best to implement it using CreateDirectoryFromApp and some loops. – Osaxely Jul 06 '22 at 07:52
  • 1
    if you have any solution please feel free post answer below. – Nico Zhu Jul 07 '22 at 00:49
  • 1
    Of course! I will post the solution as an answer as soon as it is done. – Osaxely Jul 07 '22 at 08:59

0 Answers0