这里是golang 初学者.
问题:
-
exec.Command()
为执行C:\Windows\System32\notepad.exe
而工作 - 但
exec.Command()
不适用于执行C:\Users\<username>\AppData\Local\Microsoft\WindowsApps\winget.exe
.失败并显示错误消息: - 然而,
os.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