chore: init bj_power monorepo (dashboard/mes/wms/wms_client/workstation), clear subproject git metadata and align naming to folder names
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
//go:build !windows
|
||||
|
||||
package autostart
|
||||
|
||||
func CreateDesktopShortcut(appName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Enable(appName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Disable(appName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsEnabled(appName string) bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package autostart
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
func Enable(appName string) error {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取可执行文件路径失败: %w", err)
|
||||
}
|
||||
exePath, err = filepath.Abs(exePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取绝对路径失败: %w", err)
|
||||
}
|
||||
|
||||
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开注册表失败: %w", err)
|
||||
}
|
||||
defer k.Close()
|
||||
|
||||
if err := k.SetStringValue(appName, exePath); err != nil {
|
||||
return fmt.Errorf("设置注册表值失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Disable(appName string) error {
|
||||
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开注册表失败: %w", err)
|
||||
}
|
||||
defer k.Close()
|
||||
|
||||
if err := k.DeleteValue(appName); err != nil {
|
||||
if err == registry.ErrNotExist {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("删除注册表值失败: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsEnabled(appName string) bool {
|
||||
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.READ)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer k.Close()
|
||||
|
||||
_, _, err = k.GetStringValue(appName)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func CreateDesktopShortcut(appName string) error {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取exe路径失败: %w", err)
|
||||
}
|
||||
exePath, _ = filepath.Abs(exePath)
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
shortcutPath := filepath.Join(home, "Desktop", appName+".lnk")
|
||||
|
||||
psCmd := fmt.Sprintf(
|
||||
`$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut('%s'); $s.TargetPath = '%s'; $s.WorkingDirectory = '%s'; $s.Save()`,
|
||||
shortcutPath, exePath, filepath.Dir(exePath))
|
||||
cmd := exec.Command("powershell", "-NoProfile", "-Command", psCmd)
|
||||
return cmd.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user