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
29 changes: 27 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,17 @@ public static void DeleteDirectory(string dirPath)
{
Thread.Sleep(msDelay);
}
else if (ex is System.IO.IOException)
{
string psVersion = System.Management.Automation.Runspaces.Runspace.DefaultRunspace.Version.ToString();
if (ex.Message.Contains("The directory is not empty") && psVersion.StartsWith("5"))
{
// there is a known bug with WindowsPowerShell and OneDrive based module paths, where .NET Directory.Delete() will throw a 'The directory is not empty.' error.
throw new Exception(string.Format("Cannot uninstall module with OneDrive based path on WindowsPowerShell due to .NET issue. Try installing and uninstalling using PowerShellCore if using OneDrive."), ex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Exception(string.Format("Cannot uninstall module with OneDrive based path on WindowsPowerShell due to .NET issue. Try installing and uninstalling using PowerShellCore if using OneDrive."), ex);
throw new Exception(string.Format("Cannot uninstall module with OneDrive based path on Windows PowerShell due to .NET issue. Try installing and uninstalling using PowerShell 7+ if using OneDrive."), ex);

}

throw new Exception(string.Format("Access denied to path while deleting path {0}", dirPath), ex);
}
else
{
throw;
Expand Down
Loading