| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package win
- import (
- "fmt"
- "github.com/jxeng/shortcut"
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- )
- func CreateShortCut(softPath, shortcutName string) (err error) {
- startUpFlag := false
- userHomeDir, _ := os.UserConfigDir()
- startUpList := []string{
- fmt.Sprintf(`%s\Microsoft\Windows\Start Menu\Programs\Startup`, userHomeDir),
- `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp`,
- }
- for _, startUp := range startUpList {
- handles, _ := ioutil.ReadDir(startUp)
- for _, handle := range handles {
- if handle.IsDir() {
- continue
- }
- if strings.Contains(handle.Name(), shortcutName) == true && strings.Contains(handle.Name(), ".lnk") == true {
- startUpFlag = true
- }
- }
- }
- if startUpFlag == false {
- workingDirectory, _ := filepath.Split(softPath)
- sc := shortcut.Shortcut{
- ShortcutPath: fmt.Sprintf(`%s\%s.lnk`, startUpList[0], shortcutName),
- Target: softPath,
- IconLocation: "%SystemRoot%\\System32\\SHELL32.dll,17",
- Arguments: "",
- Description: "",
- Hotkey: "",
- WindowStyle: "1",
- WorkingDirectory: workingDirectory,
- }
- err = shortcut.Create(sc)
- }
- return
- }
|