shortcut.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. _, name1 := filepath.Split(softPath)
  12. ext := filepath.Ext(name1)
  13. filename := name1[:len(name1)-len(ext)]
  14. startUpFlag := false
  15. userHomeDir, _ := os.UserConfigDir()
  16. startUpList := []string{
  17. fmt.Sprintf(`%s\Microsoft\Windows\Start Menu\Programs\Startup`, userHomeDir),
  18. `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp`,
  19. }
  20. for _, startUp := range startUpList {
  21. handles, _ := ioutil.ReadDir(startUp)
  22. for _, handle := range handles {
  23. if handle.IsDir() {
  24. continue
  25. }
  26. if strings.Contains(handle.Name(), filename) == true && strings.Contains(handle.Name(), ".lnk") == true {
  27. startUpFlag = true
  28. }
  29. }
  30. }
  31. if startUpFlag == false {
  32. workingDirectory, _ := filepath.Split(softPath)
  33. sc := shortcut.Shortcut{
  34. ShortcutPath: fmt.Sprintf(`%s\%s.lnk`, startUpList[0], shortcutName),
  35. Target: softPath,
  36. IconLocation: "%SystemRoot%\\System32\\SHELL32.dll,17",
  37. Arguments: "",
  38. Description: "",
  39. Hotkey: "",
  40. WindowStyle: "1",
  41. WorkingDirectory: workingDirectory,
  42. }
  43. err = shortcut.Create(sc)
  44. }
  45. return
  46. }