Search code examples
c#microsoft-metrowindows-runtime

Cleanup storageFolder.RoamingFolder all files winrt


I would like to cleanup all files from the storageFolder.RoamingFolder in winrt I can delete individual files but . does not work any ideas on how to aproach this ?


Solution

  • If deleting the contents of a folder is what you want to achieve a recursive method like this would work:

    public static async Task DeleteFolderContentsAsync(StorageFolder folder,
       StorageDeleteOption option)
    {
       // Try to delete all files
       var files = await folder.GetFilesAsync();
       foreach (var file in files)
       {
          await file.DeleteAsync(option);
       }
    
       // Iterate through all subfolders
       var subFolders = await folder.GetFoldersAsync();
       foreach (var subFolder in subFolders)
       {
          // Delete the contents
          await DeleteFolderContentsAsync(subFolder, option);
    
          // Delete the subfolder
          await subFolder.DeleteAsync(option);
       }
    }