Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,16 @@ public static void DeleteDirectoryWithRestore(string dirPath)
}
}

private static void SetAttributesHelper(DirectoryInfo directory)
{
foreach (var subDirectory in directory.GetDirectories())
{
subDirectory.Attributes = FileAttributes.Normal;
SetAttributesHelper(subDirectory);
}

directory.Attributes = FileAttributes.Normal;
}
/// <Summary>
/// Deletes a directory and its contents
/// This is a workaround for .NET Directory.Delete(), which can fail with WindowsPowerShell
Expand All @@ -1672,21 +1682,25 @@ public static void DeleteDirectory(string dirPath)
}

// Remove read only file attributes first
foreach (var dirFilePath in Directory.GetFiles(dirPath,"*",SearchOption.AllDirectories))
foreach (var dirFilePath in Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories))
{
if (File.GetAttributes(dirFilePath).HasFlag(FileAttributes.ReadOnly))
{
File.SetAttributes(dirFilePath, File.GetAttributes(dirFilePath) & ~FileAttributes.ReadOnly);
}
}

DirectoryInfo rootDir = new DirectoryInfo(dirPath);
SetAttributesHelper(rootDir);

// Delete directory recursive, try multiple times before throwing ( #1662 )
int maxAttempts = 5;
int msDelay = 5;
for (int attempt = 1; attempt <= maxAttempts; ++attempt)
{
try
{
Directory.Delete(dirPath,true);
Directory.Delete(dirPath, true);
return;
}
catch (Exception ex)
Expand All @@ -1695,6 +1709,10 @@ public static void DeleteDirectory(string dirPath)
{
Thread.Sleep(msDelay);
}
else if (ex is System.IO.IOException)
{
throw new Exception(string.Format("Access denied to path while deleting path {0}", dirPath), ex);
}
else
{
throw;
Expand Down
Loading