Initial GoWallpaper implementation
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
const (
|
||||
runKeyPath = `Software\Microsoft\Windows\CurrentVersion\Run`
|
||||
runValueName = "GoWallpaper"
|
||||
)
|
||||
|
||||
func setStartWithWindows(enabled bool) error {
|
||||
key, err := registry.OpenKey(registry.CURRENT_USER, runKeyPath, registry.QUERY_VALUE|registry.SET_VALUE)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开开机启动设置: %w", err)
|
||||
}
|
||||
defer key.Close()
|
||||
if !enabled {
|
||||
if err := key.DeleteValue(runValueName); err != nil && err != registry.ErrNotExist {
|
||||
return fmt.Errorf("删除开机启动项: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取程序路径: %w", err)
|
||||
}
|
||||
command := `"` + strings.ReplaceAll(exe, `"`, `\"`) + `"`
|
||||
if err := key.SetStringValue(runValueName, command); err != nil {
|
||||
return fmt.Errorf("写入开机启动项: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user