Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (77 loc) · 2.98 KB
/
Copy pathProgram.cs
File metadata and controls
87 lines (77 loc) · 2.98 KB
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
using System.Diagnostics;
using System.Reflection;
using System.Text;
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Give My Control Panel Back v1.0.0\n");
using var mutex = new Mutex(true, @"Local\GiveMyControlPanelBack", out var firstInstance);
if (!firstInstance)
{
Console.WriteLine("Another instance is already running.");
return 0;
}
var workDirectory = Path.Combine(Path.GetTempPath(), "GiveMyControlPanelBack-" + Guid.NewGuid().ToString("N"));
var exitCode = 1;
Process? child = null;
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
try
{
if (child is { HasExited: false })
child.Kill(entireProcessTree: true);
}
catch (InvalidOperationException) { }
catch (System.ComponentModel.Win32Exception) { }
};
try
{
if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17763) ||
System.Runtime.InteropServices.RuntimeInformation.OSArchitecture != System.Runtime.InteropServices.Architecture.X64)
throw new PlatformNotSupportedException("Windows 10 version 1809 or later (x64) is required.");
Directory.CreateDirectory(workDirectory);
var scriptPath = Path.Combine(workDirectory, "Restore.ps1");
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Restore.ps1")
?? throw new InvalidOperationException("The embedded installer is missing."))
using (var file = File.Create(scriptPath))
resource.CopyTo(file);
var powerShell = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System),
@"WindowsPowerShell\v1.0\powershell.exe");
if (!File.Exists(powerShell))
throw new FileNotFoundException("Windows PowerShell is missing. Restore it before running this tool.");
var start = new ProcessStartInfo(powerShell) { UseShellExecute = false };
start.Environment["PSModulePath"] = Path.Combine(Path.GetDirectoryName(powerShell)!, "Modules");
foreach (var argument in new[] { "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass",
"-File", scriptPath, "-WorkDirectory", workDirectory })
start.ArgumentList.Add(argument);
child = Process.Start(start) ?? throw new InvalidOperationException("Could not start Windows PowerShell.");
child.WaitForExit();
exitCode = child.ExitCode;
}
catch (Exception exception)
{
Console.Error.WriteLine(exception.Message);
}
finally
{
child?.Dispose();
child = null;
try
{
if (Directory.Exists(workDirectory))
Directory.Delete(workDirectory, recursive: true);
}
catch (IOException) { Console.Error.WriteLine($"Temporary files remain at: {workDirectory}"); }
catch (UnauthorizedAccessException) { Console.Error.WriteLine($"Temporary files remain at: {workDirectory}"); }
mutex.ReleaseMutex();
}
if (!Console.IsInputRedirected)
{
if (exitCode == 0)
Thread.Sleep(3000);
else
{
Console.WriteLine("\nPress any key to close.");
Console.ReadKey(intercept: true);
}
}
return exitCode;