LenovoLegionToolkit

Форк
0
86 строк · 2.9 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics;
4
using System.IO;
5
using System.Linq;
6
using System.Threading.Tasks;
7
using LenovoLegionToolkit.Lib.System;
8

9
namespace LenovoLegionToolkit.Lib.SoftwareDisabler;
10

11
public class FnKeysDisabler : AbstractSoftwareDisabler
12
{
13
    protected override IEnumerable<string> ScheduledTasksPaths => Array.Empty<string>();
14
    protected override IEnumerable<string> ServiceNames => new[] { "LenovoFnAndFunctionKeys" };
15
    protected override IEnumerable<string> ProcessNames => new[] { "LenovoUtilityUI", "LenovoUtilityService", "LenovoSmartKey" };
16

17
    public override async Task EnableAsync()
18
    {
19
        await base.EnableAsync().ConfigureAwait(false);
20
        SetUwpStartup("LenovoUtility", "LenovoUtilityID", true);
21
    }
22

23
    public override async Task DisableAsync()
24
    {
25
        await base.DisableAsync().ConfigureAwait(false);
26
        SetUwpStartup("LenovoUtility", "LenovoUtilityID", false);
27
    }
28

29
    protected override IEnumerable<string> RunningProcesses()
30
    {
31
        var result = base.RunningProcesses().ToList();
32

33
        try
34
        {
35
            foreach (var process in Process.GetProcessesByName("utility"))
36
            {
37
                var description = process.MainModule?.FileVersionInfo.FileDescription;
38
                if (description is null)
39
                    continue;
40

41
                if (description.Equals("Lenovo Hotkeys", StringComparison.InvariantCultureIgnoreCase))
42
                    result.Add(process.ProcessName);
43
            }
44
        }
45
        catch {  /* Ignored. */ }
46

47
        return result;
48
    }
49

50
    protected override async Task KillProcessesAsync()
51
    {
52
        await base.KillProcessesAsync().ConfigureAwait(false);
53

54
        try
55
        {
56
            foreach (var process in Process.GetProcessesByName("utility"))
57
            {
58
                var description = process.MainModule?.FileVersionInfo.FileDescription;
59
                if (description is null)
60
                    continue;
61

62
                if (!description.Equals("Lenovo Hotkeys", StringComparison.InvariantCultureIgnoreCase))
63
                    continue;
64

65
                process.Kill();
66
                await process.WaitForExitAsync().ConfigureAwait(false);
67
            }
68
        }
69
        catch {  /* Ignored. */ }
70
    }
71

72
    private static void SetUwpStartup(string appPattern, string subKeyName, bool enabled)
73
    {
74
        const string hive = "HKEY_CURRENT_USER";
75
        const string subKey = @"Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData";
76
        const string valueName = "State";
77

78
        var startupKey = Registry.GetSubKeys(hive, subKey).FirstOrDefault(s => s.Contains(appPattern, StringComparison.CurrentCultureIgnoreCase));
79
        if (startupKey is null)
80
            return;
81

82
        startupKey = Path.Combine(startupKey, subKeyName);
83

84
        Registry.SetValue(hive, startupKey, valueName, enabled ? 0x2 : 0x1);
85
    }
86
}
87

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.