我正在開發一個 CLI 工具,它可以讓您輕松創建檔案并在某個標志為真時打開它。這是我看到的在文本編輯器中打開檔案的唯一方法:
cmd := exec.Command(file)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
fmt.Printf("Start failed: %s", err)
}
fmt.Printf("Waiting for command to finish.\n")
err = cmd.Wait()
fmt.Printf("Command finished with error: %v\n", err)
...順便說一下,我不太明白,并收到以下錯誤:
Start failed: fork/exec H:/code/txt_projects/hello6.txt: %1 is not a valid Win32 application.Waiting for command to finish.
Command finished with error: exec: not started
我該如何解決?如何使用例如記事本打開檔案?
uj5u.com熱心網友回復:
你應該呼叫exec.Command一個字串,它的內容是一個可執行的命令路徑,這個命令的一些引數作為其他引數,比如:
// shortcut for command in ENV PATH
//exepath := "notepad"
exepath := "C:\\Windows\\system32\\notepad.exe"
file := "H:\\code\\txt_projects\\hello6.txt"
cmd := exec.Command(exepath, file)
請參閱檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318444.html
上一篇:使用lua從文本檔案中取回檔案名
