-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemLockTaskService.cs
More file actions
42 lines (37 loc) · 1.37 KB
/
Copy pathSystemLockTaskService.cs
File metadata and controls
42 lines (37 loc) · 1.37 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
using System.ComponentModel;
using System.Diagnostics;
namespace BatteryDown;
internal static class SystemLockTaskService
{
public static void Configure(bool enabled)
{
if (!enabled && !TaskSchedulerService.IsLockedSystemTaskInstalled)
{
return;
}
var executablePath = Environment.ProcessPath
?? throw new InvalidOperationException("无法确定程序路径。");
try
{
using var process = Process.Start(new ProcessStartInfo
{
FileName = executablePath,
Arguments = enabled
? "--configure-lock-task enable"
: "--configure-lock-task disable",
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Hidden,
}) ?? throw new InvalidOperationException("无法启动锁屏系统任务配置程序。");
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new InvalidOperationException("锁屏系统任务配置失败。");
}
}
catch (Win32Exception exception) when (exception.NativeErrorCode == 1223)
{
throw new OperationCanceledException("已取消管理员授权,锁屏立即关机功能尚未启用。", exception);
}
}
}