exec.Command()用于执行C:\Windows\System32\notepad.exeexec.Command()不适用于执行C:\Users\<username>\AppData\Local\Microsoft\WindowsApps\winget.exe。错误消息失败:exec: "C:\\Users\\<username>\\AppData\\Local\\Microsoft\\WindowsApps\\winget.exe": file does not existos.StartProcess()用于执行C:\Users\<username>\AppData\Local\Microsoft\WindowsApps\winget.exe。谁能告诉我原因吗?
此代码片段不起作用。winget.exe还没有发布。
wingetPath := filepath.Join(os.Getenv("LOCALAPPDATA"),
"Microsoft\\WindowsApps\\winget.exe")
cmd := exec.Command(wingetPath, "--version")
err := cmd.Start()
fmt.Println(err)
// exec: "C:\\Users\\<username>\\AppData\\Local\\Microsoft\\WindowsApps\\winget.exe": file does not exist但这样做是可行的:
wingetPath := filepath.Join(os.Getenv("LOCALAPPDATA"),
"Microsoft\\WindowsApps\\winget.exe")
procAttr := new(os.ProcAttr)
procAttr.Files = []*os.File{nil, nil, nil}
// The argv slice will become os.Args in the new process,
// so it normally starts with the program name
_, err := os.StartProcess(wingetPath, []string{wingetPath, "--version"}, procAttr)
fmt.Println(err)
// <nil>Go版本:
> go version
go version go1.18 windows/amd64发布于 2022-04-14 16:09:10
戈朗虫
因此,很明显,这是Go的Windows实现中的一个bug,并多次在GitHub上注册--我能找到的最古老的问题是几年前提交的这个问题。
该错误是由于exec.Command()内部使用 os.Stat()无法正确读取修复点文件造成的。os.Lstat()可以。
Windows应用程序使用App执行别名,它本质上是具有解析点的零字节文件。这个帖子有一些额外的细节。
解决办法
os.StartProces() --这是一个较低级别的API,使用起来可能有点痛苦,特别是与os.Exec()相比。
:在os.StartProcess()中,argv片将成为新进程中的os.Args,因此通常应该传递程序名作为第一个参数:wingetPath := filepath.Join(os.Getenv("LOCALAPPDATA"),
"Microsoft\\WindowsApps\\winget.exe")
procAttr := new(os.ProcAttr)
procAttr.Files = []*os.File{nil, nil, nil}
/*
To redirect IO, pass in stdin, stdout, stderr as required
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
*/
args = []string { "install", "git.git" }
// The argv slice will become os.Args in the new process,
// so it normally starts with the program name
proc, err := os.StartProcess(wingetPath,
append([]string{wingetPath}, arg...), procAttr)
fmt.Println(err) // nilhttps://stackoverflow.com/questions/71865309
复制相似问题