Using UWP File IO methods in Unity - works with HoloLens 2

The trick here is to convert an async method to a Coroutine.
Do stuff in another thread, yield-return-null while the thread is busy, and use an Action to "return" any values you like.

public static IEnumerator DeleteFolder(IStorageFolder folder, Task<string> onCompleteTask)
 {
  string error = null;

#if WINDOWS_UWP         
 Task theTask = Task.Run(async () => {
        try 
        {
           await folder.DeleteAsync(StorageDeleteOption.PermanentDelete);  
        } 
        catch (Exception ex)
        { 
   error = ex.ToString(); 
 }});

        while(!theTask.IsCompleted)
            yield return null;
#endif        
 onCompleteTask(error);
    yield break; 
} 

Usage:

yield return MyFileUtils.DeleteFolder(theStorageFolderToDelete,
     (error) => { if (!string.IsNullOrEmpty(error)) Log(error); });



Comments