-
Notifications
You must be signed in to change notification settings - Fork 1
/
autostart.go
67 lines (52 loc) · 1.44 KB
/
autostart.go
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
package main
import (
"fmt"
"golang.org/x/sys/windows/registry"
)
const registryName = "steam-account-switcher"
func isAutoStartActive() (bool, error) {
reg, err := getAutoStartRegistry()
if err != nil {
return false, fmt.Errorf("could not get registry: %w", err)
}
val, _, err := reg.GetStringValue(registryName)
if err != nil {
if err == registry.ErrNotExist {
return false, nil
}
return false, fmt.Errorf("could not get value: %w", err)
}
if val != executablePath {
// path changed to executable, update it
err = reg.SetStringValue(registryName, executablePath)
if err != nil {
return false, fmt.Errorf("could not update autostart entry: %w", err)
}
}
return true, nil
}
func enableAutoStart() error {
reg, err := getAutoStartRegistry()
if err != nil {
return fmt.Errorf("could not get registry: %w", err)
}
err = reg.SetStringValue(registryName, executablePath)
if err != nil {
return fmt.Errorf("could not set autostart entry: %w", err)
}
return nil
}
func disableAutoStart() error {
reg, err := getAutoStartRegistry()
if err != nil {
return fmt.Errorf("could not get registry: %w", err)
}
err = reg.DeleteValue(registryName)
if err != nil {
return fmt.Errorf("could not remove autostart entry: %w", err)
}
return nil
}
func getAutoStartRegistry() (registry.Key, error) {
return registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.READ|registry.WRITE)
}