I am trying to write code to return the path to the default browser on windows system.
I have run this command in the temrinal :
for /f "tokens=3" %a in ('reg QUERY HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice /v ProgId') do reg QUERY HKCR\%a\shell\open\command
and got:
(Default) REG_SZ "C:\Program Files\Google\Chrome\Application\chrome.exe" --single-argument %1
Then I tried to use this in the Go code like that :
func main() {
var c *exec.Cmd
c = exec.Command("cmd", "for /f \"tokens=3\" %a in ('reg QUERY HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.html\\UserChoice /v ProgId') do reg QUERY HKCR\\%a\\shell\\open\\command")
if output, err := c.CombinedOutput(); err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println(string(output))
}`your text`
}
but instead I got :
Microsoft Windows [Version 10.0.19045.3448]
(c) Microsoft Corporation. All rights reserved.
How can i get get same answer of terminal using Go?
You could call a bunch of Win32 API functions provided by the standard
syscallpackage.A demo program which queries the (string) value of
HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop:I do not have the keys you're interested in on my system, so I tested whatever I have. It's trivial to adapt this to your needs.
You can start by reading the docs on
RegQueryValueEx.