-
米琪卡哇伊
使用https://github.com/go-ole/go-ole:ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_SPEED_OVER_MEMORY)oleShellObject, err := oleutil.CreateObject("WScript.Shell")if err != nil { return err}defer oleShellObject.Release()wshell, err := oleShellObject.QueryInterface(ole.IID_IDispatch)if err != nil { return err}defer wshell.Release()cs, err := oleutil.CallMethod(wshell, "CreateShortcut", dst)if err != nil { return err}idispatch := cs.ToIDispatch()oleutil.PutProperty(idispatch, "TargetPath", src)oleutil.CallMethod(idispatch, "Save")
-
呼唤远方
使用 VBS的AWFUL Working golang 解决方案;package mainimport( "bytes" "fmt" "io/ioutil" "os" "os/exec")func createShortcut(linkName string, target string, arguments string, directory string, description string, destination string) { var scriptTxt bytes.Buffer scriptTxt.WriteString("option explicit\n\n") scriptTxt.WriteString("sub CreateShortCut()\n") scriptTxt.WriteString("dim objShell, strDesktopPath, objLink\n") scriptTxt.WriteString("set objShell = CreateObject(\"WScript.Shell\")\n") scriptTxt.WriteString("strDesktopPath = objShell.SpecialFolders(\"") scriptTxt.WriteString(destination) scriptTxt.WriteString("\")\n") scriptTxt.WriteString("set objLink = objShell.CreateShortcut(strDesktopPath & \"\\") scriptTxt.WriteString(linkName) scriptTxt.WriteString(".lnk\")\n") scriptTxt.WriteString("objLink.Arguments = \"") scriptTxt.WriteString(arguments) scriptTxt.WriteString("\"\n") scriptTxt.WriteString("objLink.Description = \"") scriptTxt.WriteString(description) scriptTxt.WriteString("\"\n") scriptTxt.WriteString("objLink.TargetPath = \"") scriptTxt.WriteString(target) scriptTxt.WriteString("\"\n") scriptTxt.WriteString("objLink.WindowStyle = 1\n") scriptTxt.WriteString("objLink.WorkingDirectory = \"") scriptTxt.WriteString(directory) scriptTxt.WriteString("\"\n") scriptTxt.WriteString("objLink.Save\nend sub\n\n") scriptTxt.WriteString("call CreateShortCut()") fmt.Print(scriptTxt.String()) filename := fmt.Sprintf("lnkTo%s.vbs", destination) ioutil.WriteFile(filename, scriptTxt.Bytes(), 0777) cmd := exec.Command("wscript", filename) err := cmd.Run() if err != nil { fmt.Println(err) } cmd.Wait() os.Remove(filename) return}
-
大话西游666
如果出于任何原因您不想使用外部 go 包,这是一种替代方法。正如Alexis Paques所提到的,您可以使用 Powershell 在 Windows 下创建快捷方式。优点是,它已经在几乎所有 Windows 环境中可用。这是在 shell:startup 文件夹中创建快捷方式的实现,它将在启动时为当前用户自动启动链接程序:package mainimport ( "bytes" "log" "os/exec" "strings")type PowerShell struct { powerShell string}var WIN_CREATE_SHORTCUT = `$WshShell = New-Object -comObject WScript.Shell$Shortcut = $WshShell.CreateShortcut("$HOME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\MyAPP.lnk")$Shortcut.TargetPath = "PLACEHOLDER"$Shortcut.Save()`// New create new sessionfunc New() *PowerShell { ps, _ := exec.LookPath("powershell.exe") return &PowerShell{ powerShell: ps, }}func (p *PowerShell) execute(args ...string) (stdOut string, stdErr string, err error) { args = append([]string{"-NoProfile", "-NonInteractive"}, args...) cmd := exec.Command(p.powerShell, args...) var stdout bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr err = cmd.Run() stdOut, stdErr = stdout.String(), stderr.String() return}// enableAutostartWin creates a shortcut to MyAPP in the shell:startup folderfunc enableAutostartWin() { ps := New() exec_path := "C:\\MyAPP.exe" WIN_CREATE_SHORTCUT = strings.Replace(WIN_CREATE_SHORTCUT, "PLACEHOLDER", exec_path, 1) stdOut, stdErr, err := ps.execute(WIN_CREATE_SHORTCUT) log.Printf("CreateShortcut:\nStdOut : '%s'\nStdErr: '%s'\nErr: %s", strings.TrimSpace(stdOut), stdErr, err)}这个答案基于这个 SO answer和这个gist。