LenovoLegionToolkit
86 строк · 2.9 Кб
1using System;2using System.Collections.Generic;3using System.Diagnostics;4using System.IO;5using System.Linq;6using System.Threading.Tasks;7using LenovoLegionToolkit.Lib.System;8
9namespace LenovoLegionToolkit.Lib.SoftwareDisabler;10
11public class FnKeysDisabler : AbstractSoftwareDisabler12{
13protected override IEnumerable<string> ScheduledTasksPaths => Array.Empty<string>();14protected override IEnumerable<string> ServiceNames => new[] { "LenovoFnAndFunctionKeys" };15protected override IEnumerable<string> ProcessNames => new[] { "LenovoUtilityUI", "LenovoUtilityService", "LenovoSmartKey" };16
17public override async Task EnableAsync()18{19await base.EnableAsync().ConfigureAwait(false);20SetUwpStartup("LenovoUtility", "LenovoUtilityID", true);21}22
23public override async Task DisableAsync()24{25await base.DisableAsync().ConfigureAwait(false);26SetUwpStartup("LenovoUtility", "LenovoUtilityID", false);27}28
29protected override IEnumerable<string> RunningProcesses()30{31var result = base.RunningProcesses().ToList();32
33try34{35foreach (var process in Process.GetProcessesByName("utility"))36{37var description = process.MainModule?.FileVersionInfo.FileDescription;38if (description is null)39continue;40
41if (description.Equals("Lenovo Hotkeys", StringComparison.InvariantCultureIgnoreCase))42result.Add(process.ProcessName);43}44}45catch { /* Ignored. */ }46
47return result;48}49
50protected override async Task KillProcessesAsync()51{52await base.KillProcessesAsync().ConfigureAwait(false);53
54try55{56foreach (var process in Process.GetProcessesByName("utility"))57{58var description = process.MainModule?.FileVersionInfo.FileDescription;59if (description is null)60continue;61
62if (!description.Equals("Lenovo Hotkeys", StringComparison.InvariantCultureIgnoreCase))63continue;64
65process.Kill();66await process.WaitForExitAsync().ConfigureAwait(false);67}68}69catch { /* Ignored. */ }70}71
72private static void SetUwpStartup(string appPattern, string subKeyName, bool enabled)73{74const string hive = "HKEY_CURRENT_USER";75const string subKey = @"Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData";76const string valueName = "State";77
78var startupKey = Registry.GetSubKeys(hive, subKey).FirstOrDefault(s => s.Contains(appPattern, StringComparison.CurrentCultureIgnoreCase));79if (startupKey is null)80return;81
82startupKey = Path.Combine(startupKey, subKeyName);83
84Registry.SetValue(hive, startupKey, valueName, enabled ? 0x2 : 0x1);85}86}
87