-
Notifications
You must be signed in to change notification settings - Fork 2
/
CleanupCommand.cs
98 lines (88 loc) · 3.37 KB
/
CleanupCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Thomas Gossler. All rights reserved.
// Licensed under the MIT license.
#nullable disable warnings
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security;
using Microsoft.Win32;
using Spectre.Console;
using Spectre.Console.Cli;
namespace WebPageHost;
/// <summary>
/// Cleans-up user resources in file system and Windows registry.
/// </summary>
internal sealed partial class CleanupCommand : Command<CleanupCommandSettings>
{
/// <summary>
/// Cleanup command handler
/// </summary>
/// <param name="context">The command context.</param>
/// <param name="settings">The provided command line arguments.</param>
/// <returns>Program exit code</returns>
public override int Execute([NotNull] CommandContext context, [NotNull] CleanupCommandSettings settings)
{
// Get WebView2 user folder name
string userDataFolderName = Common.WebView2UserDataFolderName;
// Delete WebView2 data folder of the current user
AnsiConsole.Markup($"Removing the current user's web browser persistent data folder... ");
DeleteWebView2UserDataFolder(userDataFolderName);
AnsiConsole.MarkupLine($"[green]Done[/].");
// Delete program registry seetings for the current user
AnsiConsole.Markup($"Removing the current user's registry settings for this program... ");
DeleteRegistrySettings();
AnsiConsole.MarkupLine($"[green]Done[/].");
return 0;
}
/// <summary>
/// Delete the WebView2 data folder for the current user.
/// </summary>
/// <param name="userDataFolderName">Folder name without path, relative to the folder of the program executable.</param>
private static void DeleteWebView2UserDataFolder(string userDataFolderName)
{
try {
string exeFilePath = AppContext.BaseDirectory;
string exeDirPath = Path.GetDirectoryName(exeFilePath);
var dirInfo = new DirectoryInfo(exeDirPath);
var userDataFolder = (DirectoryInfo)dirInfo.GetDirectories(userDataFolderName).GetValue(0);
if (null != userDataFolder) {
bool success = false;
try {
if (userDataFolder.Exists) {
userDataFolder.Delete(true);
}
success = !userDataFolder.Exists;
}
catch (UnauthorizedAccessException) { }
catch (DirectoryNotFoundException) { }
catch (IOException) { }
catch (SecurityException) { }
if (!success) {
Trace.TraceWarning(string.Format("ERROR: WebView2 user data folder '{0}' could not be deleted", userDataFolder.FullName));
}
}
}
catch (Exception ex) {
Debug.WriteLine(ex.Message);
}
}
/// <summary>
/// Delete the program registry settings for the current user.
/// </summary>
private static void DeleteRegistrySettings()
{
try {
Registry.CurrentUser.DeleteSubKeyTree(Common.ProgramRegistryRootKeyPath);
}
catch (Exception ex) {
Debug.WriteLine(ex.Message);
}
}
}
/// <summary>
/// Command line arguments for the cleanup command.
/// </summary>
public sealed class CleanupCommandSettings : CommandSettings
{
}