shortcut.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package win
  2. import (
  3. "fmt"
  4. "github.com/jxeng/shortcut"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. func CreateShortCut(softPath, shortcutName string) (err error) {
  11. startUpFlag := false
  12. userHomeDir, _ := os.UserConfigDir()
  13. startUpList := []string{
  14. fmt.Sprintf(`%s\Microsoft\Windows\Start Menu\Programs\Startup`, userHomeDir),
  15. `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp`,
  16. }
  17. for _, startUp := range startUpList {
  18. handles, _ := ioutil.ReadDir(startUp)
  19. for _, handle := range handles {
  20. if handle.IsDir() {
  21. continue
  22. }
  23. if strings.Contains(handle.Name(), shortcutName) == true && strings.Contains(handle.Name(), ".lnk") == true {
  24. startUpFlag = true
  25. }
  26. }
  27. }
  28. if startUpFlag == false {
  29. workingDirectory, _ := filepath.Split(softPath)
  30. sc := shortcut.Shortcut{
  31. ShortcutPath: fmt.Sprintf(`%s\%s.lnk`, startUpList[0], shortcutName),
  32. Target: softPath,
  33. IconLocation: "%SystemRoot%\\System32\\SHELL32.dll,17",
  34. Arguments: "",
  35. Description: "",
  36. Hotkey: "",
  37. WindowStyle: "1",
  38. WorkingDirectory: workingDirectory,
  39. }
  40. err = shortcut.Create(sc)
  41. }
  42. return
  43. }