-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed IOException - directory not empty error when deleting cache folder
- Loading branch information
Showing
3 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace SharedUtils | ||
{ | ||
public static class FileUtils | ||
{ | ||
public static async Task<bool> DeleteDirectory(string directoryPath, int maxRetries = 10, int millisecondsDelay = 30) | ||
{ | ||
if (directoryPath == null) | ||
throw new ArgumentNullException(directoryPath); | ||
if (maxRetries < 1) | ||
throw new ArgumentOutOfRangeException(nameof(maxRetries)); | ||
if (millisecondsDelay < 1) | ||
throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); | ||
|
||
for (int i = 0; i < maxRetries; ++i) { | ||
try { | ||
// try to delete the directory if it exists | ||
if (Directory.Exists(directoryPath)) | ||
Directory.Delete(directoryPath, true); | ||
|
||
return true; | ||
} catch (IOException) { // System.IO.IOException: The directory is not empty | ||
await Task.Delay(millisecondsDelay); | ||
} catch (UnauthorizedAccessException) { | ||
await Task.Delay(millisecondsDelay); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters