Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (142 loc) · 4.86 KB
/
Copy pathProgram.cs
File metadata and controls
151 lines (142 loc) · 4.86 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Diagnostics;
namespace ChromeUpdateToggle;
static class Program
{
private const string ElevatedFlag = "CUT_ELEVATED";
[STAThread]
static int Main(string[] args)
{
if (args.Any(a => a is "--disable" or "1"))
return RunAction(l => UpdateManager.Disable(l), args);
if (args.Any(a => a is "--enable" or "2"))
return RunAction(l => UpdateManager.Enable(l), args);
if (args.Any(a => a is "--reset" or "3"))
return RunAction(l => UpdateManager.ResetToDefaults(l), args);
if (args.Any(a => a is "--lang"))
{
string v = args.SkipWhile(a => a != "--lang").Skip(1).FirstOrDefault() ?? "zh";
Strings.SetLang(v.StartsWith("en", StringComparison.OrdinalIgnoreCase) ? "en" : "zh");
Console.WriteLine("lang=" + Strings.Current);
return 0;
}
if (args.Any(a => a is "--export-diagnostics"))
{
// 导出诊断包(只读为主, 不提权)
try
{
string zip = args.SkipWhile(a => a != "--export-diagnostics").Skip(1)
.FirstOrDefault()
?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Strings.DiagDefaultName);
Logger.ExportDiagnostics(zip, Logger.Tee(Console.WriteLine));
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine("[ERROR] " + ex.Message);
return 2;
}
}
if (args.Any(a => a is "--status"))
{
// 只读查询, 不提权
try
{
var (status, detail) = UpdateManager.GetUpdateStatus();
Console.WriteLine($"{status} ({detail})");
Console.WriteLine(UpdateManager.DescribeState());
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine("[ERROR] " + ex.Message);
return 2;
}
}
ApplicationConfiguration.Initialize();
Application.Run(new MainForm());
return 0;
}
/// <summary>写操作: 非管理员时自动拉起提权副本执行, 用户只需点一次 UAC。</summary>
private static int RunAction(Action<Action<string>> action, string[] args)
{
if (UpdateManager.IsAdministrator())
{
var tee = Logger.Tee(Console.WriteLine);
try
{
action(tee);
return 0;
}
catch (Exception ex)
{
tee("[ERROR] " + ex.Message);
Logger.Log(ex.ToString());
return 2;
}
}
if (Environment.GetEnvironmentVariable(ElevatedFlag) == "1")
{
Console.Error.WriteLine(Strings.ErrDenied);
return 1;
}
try
{
var psi = new ProcessStartInfo
{
FileName = Environment.ProcessPath!,
Arguments = string.Join(" ", args.Select(a => $"\"{a}\"")),
UseShellExecute = true,
Verb = "runas",
};
psi.Environment[ElevatedFlag] = "1";
using var p = Process.Start(psi)!;
p.WaitForExit();
return p.ExitCode;
}
catch (System.ComponentModel.Win32Exception ex) when (ex.NativeErrorCode == 1223)
{
Console.Error.WriteLine(Strings.ErrCancelled);
return 1;
}
}
/// <summary>供 UI 调用: 非管理员时提权执行指定动作, 回传退出码。</summary>
public static int RunElevated(string actionArg, Action<string>? log = null)
{
if (UpdateManager.IsAdministrator())
{
// UI 传的 Log 自带落盘, CLI 才包 Tee
var write = log ?? Logger.Tee(Console.WriteLine);
try
{
if (actionArg == "--disable") UpdateManager.Disable(write);
else UpdateManager.Enable(write);
return 0;
}
catch (Exception ex)
{
write("[ERROR] " + ex.Message);
Logger.Log(ex.ToString());
return 2;
}
}
try
{
var psi = new ProcessStartInfo
{
FileName = Environment.ProcessPath!,
Arguments = $"\"{actionArg}\"",
UseShellExecute = true,
Verb = "runas",
};
psi.Environment[ElevatedFlag] = "1";
using var p = Process.Start(psi)!;
p.WaitForExit();
return p.ExitCode;
}
catch (System.ComponentModel.Win32Exception ex) when (ex.NativeErrorCode == 1223)
{
return 1;
}
}
}