Skip to content

Instantly share code, notes, and snippets.

@bear256
Last active October 19, 2022 18:47
Show Gist options
  • Save bear256/b695d2f4d45262d51c91e04d12930d43 to your computer and use it in GitHub Desktop.
Save bear256/b695d2f4d45262d51c91e04d12930d43 to your computer and use it in GitHub Desktop.
[Windows Registry Query & Find Chrome Path] Query value by key from Windows Registry Table, and find the path of Chrome installed on local Windows machine #Golang
package main
import (
"fmt"
"golang.org/x/sys/windows/registry"
)
// QueryRegistry to query the value by registry key, path and name
func QueryRegistry(key registry.Key, path, name string) (string, uint32, error) {
regKey, err := registry.OpenKey(key, path, registry.QUERY_VALUE)
if err != nil {
return "", 0, err
}
defer regKey.Close()
val, typ, err := regKey.GetStringValue(name)
return val, typ, err
}
// FindChromeInstalledPath to find the path of Chrome installed on local Windows machine
func FindChromeInstalledPath() string {
var chromeInstalledPath string
keys := []registry.Key{registry.CURRENT_USER, registry.LOCAL_MACHINE}
path := `SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe`
for _, key := range keys {
val, _, err := QueryRegistry(key, path, "")
if err != nil {
continue
} else {
chromeInstalledPath = val
break
}
}
return chromeInstalledPath
}
func main() {
path := FindChromeInstalledPath()
fmt.Println(path)
}
@asmirbe
Copy link

asmirbe commented Oct 19, 2022

thank you, so much :D !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment