-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (62 loc) · 2.74 KB
/
Copy pathProgram.cs
File metadata and controls
74 lines (62 loc) · 2.74 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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
namespace MyApp {
static class Program {
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
[STAThread]
private static void Main() {
_ = AllocConsole();
Console.WriteLine("DeleteNetworkX - 正在检查权限...");
if (!IsRunAsAdmin()) {
var processPath = Environment.ProcessPath
?? throw new InvalidOperationException("无法获取程序路径");
var dir = Path.GetDirectoryName(processPath) ?? Environment.CurrentDirectory;
var isDotnetHost = Path.GetFileNameWithoutExtension(processPath)
.Equals("dotnet", StringComparison.OrdinalIgnoreCase);
var startInfo = new ProcessStartInfo {
UseShellExecute = true,
WorkingDirectory = dir,
Verb = "runas",
};
if (isDotnetHost) {
startInfo.FileName = processPath;
startInfo.Arguments = $"\"{Assembly.GetExecutingAssembly().Location}\"";
} else {
startInfo.FileName = processPath;
}
try {
_ = Process.Start(startInfo);
Console.WriteLine("已请求管理员权限,请在弹出的 UAC 窗口中选择\"是\"。");
} catch (Exception ex) {
Console.WriteLine($"启动管理员进程失败: {ex.Message}");
}
Console.WriteLine("按任意键退出...");
_ = Console.ReadKey();
return;
}
Console.WriteLine("已获取管理员权限,开始清理...");
try {
string path1 = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles";
string path2 = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged";
string deletePattern = "网络";
new Reg(path1, deletePattern).Delete();
new Reg(path2, deletePattern).Delete();
Console.WriteLine("清理完成!");
} catch (Exception ex) {
Console.WriteLine($"错误: {ex.Message}");
}
Console.WriteLine("按任意键退出...");
_ = Console.ReadKey();
}
private static bool IsRunAsAdmin() {
using var wi = WindowsIdentity.GetCurrent();
var wp = new WindowsPrincipal(wi);
return wp.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}