Golang: launch default application for pdf file in windows

I want to open a PDF file in the file system, starting with the default application. How can i do this? From the command line, I simply write the name 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) } } 
+6
source share
2 answers

You should run 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") 
+1
source
 exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", "path_to_foo.pdf") 

must also handle it.

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

+11
source

Source: https://habr.com/ru/post/923475/


All Articles