Skip to content

Commit

Permalink
Fix errors when deleting folders marked as read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs00 committed Jul 4, 2019
1 parent 66747e9 commit 5fce0d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/Operations/OneDriveRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ private void DisableOneDriveViaGroupPolicies()
private void RemoveResidualFiles()
{
ConsoleUtils.WriteLine("Removing old files...", ConsoleColor.Green);
SystemUtils.DeleteDirectoryIfExists(@"C:\OneDriveTemp", handleErrors: true);
SystemUtils.DeleteDirectoryIfExists($@"{Env.GetFolderPath(Env.SpecialFolder.LocalApplicationData)}\Microsoft\OneDrive", handleErrors: true);
SystemUtils.DeleteDirectoryIfExists($@"{Env.GetFolderPath(Env.SpecialFolder.CommonApplicationData)}\Microsoft\OneDrive", handleErrors: true);
SystemUtils.DeleteDirectoryIfExists($@"{Env.GetFolderPath(Env.SpecialFolder.UserProfile)}\OneDrive", handleErrors: true);
SystemUtils.DeleteDirectoryIfExistsAndHandleErrors(@"C:\OneDriveTemp");
SystemUtils.DeleteDirectoryIfExistsAndHandleErrors($@"{Env.GetFolderPath(Env.SpecialFolder.LocalApplicationData)}\Microsoft\OneDrive");
SystemUtils.DeleteDirectoryIfExistsAndHandleErrors($@"{Env.GetFolderPath(Env.SpecialFolder.CommonApplicationData)}\Microsoft\OneDrive");
SystemUtils.DeleteDirectoryIfExistsAndHandleErrors($@"{Env.GetFolderPath(Env.SpecialFolder.UserProfile)}\OneDrive");
}

private void RemoveResidualRegistryKeys()
Expand Down
7 changes: 3 additions & 4 deletions src/Operations/UWPAppRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ private void PerformPostUninstallOperations(UWPAppGroup appGroup)
private static void RemoveEdgeResidualFiles()
{
Console.WriteLine("Removing old files...");
SystemUtils.DeleteDirectoryIfExists(
$@"{Env.GetFolderPath(Env.SpecialFolder.UserProfile)}\MicrosoftEdgeBackups",
handleErrors: true
SystemUtils.DeleteDirectoryIfExistsAndHandleErrors(
$@"{Env.GetFolderPath(Env.SpecialFolder.UserProfile)}\MicrosoftEdgeBackups"
);
}

Expand Down Expand Up @@ -262,7 +261,7 @@ private static void RemovePrint3DContextMenuEntriesAnd3DObjectsFolder()
@"\Explorer\MyComputer\NameSpace", true))
key.DeleteSubKeyTree("{0DB7E03F-FC29-4DC6-9020-FF41B59E513A}");
}
SystemUtils.DeleteDirectoryIfExists($@"{Env.GetFolderPath(Env.SpecialFolder.UserProfile)}\3D Objects", handleErrors: true);
SystemUtils.DeleteDirectoryIfExistsAndHandleErrors($@"{Env.GetFolderPath(Env.SpecialFolder.UserProfile)}\3D Objects");
}

private static void RestoreWindowsPhotoViewer()
Expand Down
34 changes: 16 additions & 18 deletions src/Utils/SystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,26 @@ private static Process CreateProcessInstance(string name, string args)
};
}

/**
* Deletes the folder passed recursively
* Can optionally handle exceptions inside its body to avoid propagating of errors
*/
public static void DeleteDirectoryIfExists(string path, bool handleErrors = false)
public static void DeleteDirectoryIfExistsAndHandleErrors(string path)
{
if (handleErrors)
try
{
try
{
if (Directory.Exists(path))
Directory.Delete(path, true);
}
catch (Exception exc)
{
ConsoleUtils.WriteLine($"An error occurred when deleting folder {path}: {exc.Message}", ConsoleColor.Red);
}
DeleteDirectoryIfExists(path);
}
else
catch (Exception exc)
{
ConsoleUtils.WriteLine($"An error occurred when deleting folder {path}: {exc.Message}", ConsoleColor.Red);
}
}

private static void DeleteDirectoryIfExists(string path)
{
var directoryToDelete = new DirectoryInfo(path);
if (directoryToDelete.Exists)
{
if (Directory.Exists(path))
Directory.Delete(path, true);
// Reset attributes of the folder (avoid errors if a folder is marked as read-only)
directoryToDelete.Attributes = FileAttributes.Directory;
directoryToDelete.Delete(recursive: true);
}
}

Expand Down

0 comments on commit 5fce0d0

Please sign in to comment.