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

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

golang: run default application for a pdf file on windows

问题

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

package main

import (
	"log"
	"os/exec"
)

func main() {
	cmd := exec.Command("foo.pdf")
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	err = cmd.Wait()
	if err != nil {
		log.Fatal(err)
	}

}
英文:

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%.

package main

import (
	"log"
	"os/exec"
)

func main() {
	cmd := exec.Command("foo.pdf")
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	err = cmd.Wait()
	if err != nil {
		log.Fatal(err)
	}

}

答案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.

英文:
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。这将让启动命令为您找到正确的可执行文件。

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.

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

答案3

得分: 0

创建一个像这样的文件:

//go:generate mkwinsyscall -output zmain.go main.go
//sys shellExecute(hwnd int, oper string, file string, param string, dir string, show int) (err error) = shell32.ShellExecuteW
package main

const sw_shownormal = 1

func main() {
   shellExecute(0, "", "file.pdf", "", "", sw_shownormal)
}

然后进行构建:

go mod init pdf
go generate
go mod tidy
go build

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

英文:

Create a file like this:

//go:generate mkwinsyscall -output zmain.go main.go
//sys shellExecute(hwnd int, oper string, file string, param string, dir string, show int) (err error) = shell32.ShellExecuteW
package main

const sw_shownormal = 1

func main() {
   shellExecute(0, "", "file.pdf", "", "", sw_shownormal)
}

Then build:

go mod init pdf
go generate
go mod tidy
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:

确定