在Windows上运行默认的PDF文件应用程序的Golang代码。

huangapple go评论122阅读模式
英文:

golang: run default application for a pdf file on windows

问题

我想要用默认应用程序从Go中的文件系统打开一个PDF文件。我该如何做到这一点?从命令行中,我只需写入pdf文件的文件名,应用程序就会打开(并显示请求的文件)。当我尝试使用exec.Command()时,我会得到一个错误(并不奇怪)exec: "foo.pdf": 在%PATH%中找不到可执行文件

  1. package main
  2. import (
  3. "log"
  4. "os/exec"
  5. )
  6. func main() {
  7. cmd := exec.Command("foo.pdf")
  8. err := cmd.Start()
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. err = cmd.Wait()
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. }
英文:

I'd like to open a PDF file in the filesystem from go with the default application. How can I do that? From the command line I just write the filename of the pdf file and the application opens (with the requested file). When I try to use exec.Command() I get an error (not surprisingly) exec: "foo.pdf": executable file not found in %PATH%.

  1. package main
  2. import (
  3. "log"
  4. "os/exec"
  5. )
  6. func main() {
  7. cmd := exec.Command("foo.pdf")
  8. err := cmd.Start()
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. err = cmd.Wait()
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. }

答案1

得分: 13

exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", "path_to_foo.pdf") should also handle it.

Note that still the right way to do it is to use a C wrapper around the ShellExecute() API function exported by shell32.dll, and the "w32" library seems to provide this wrapper right away.

英文:
  1. exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", "path_to_foo.pdf")

should also handle it.

Note that still the right way to do it is to use a C wrapper around the ShellExecute() API function exported by shell32.dll, and the "w32" library seems to provide this wrapper right away.

答案2

得分: 3

你必须启动 cmd /C start foo.pdf。这将让启动命令为您找到正确的可执行文件。

  1. cmd := exec.Command("cmd", "/C start path_to_foo.pdf")
英文:

You must launch cmd /C start foo.pdf. This will let the start command find the correct executable for you.

  1. cmd := exec.Command("cmd", "/C start path_to_foo.pdf")

答案3

得分: 0

创建一个像这样的文件:

  1. //go:generate mkwinsyscall -output zmain.go main.go
  2. //sys shellExecute(hwnd int, oper string, file string, param string, dir string, show int) (err error) = shell32.ShellExecuteW
  3. package main
  4. const sw_shownormal = 1
  5. func main() {
  6. shellExecute(0, "", "file.pdf", "", "", sw_shownormal)
  7. }

然后进行构建:

  1. go mod init pdf
  2. go generate
  3. go mod tidy
  4. go build

https://pkg.go.dev/golang.org/x/sys/windows/mkwinsyscall

英文:

Create a file like this:

  1. //go:generate mkwinsyscall -output zmain.go main.go
  2. //sys shellExecute(hwnd int, oper string, file string, param string, dir string, show int) (err error) = shell32.ShellExecuteW
  3. package main
  4. const sw_shownormal = 1
  5. func main() {
  6. shellExecute(0, "", "file.pdf", "", "", sw_shownormal)
  7. }

Then build:

  1. go mod init pdf
  2. go generate
  3. go mod tidy
  4. go build

https://pkg.go.dev/golang.org/x/sys/windows/mkwinsyscall

huangapple
  • 本文由 发表于 2012年8月22日 20:54:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/12073635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定