Last active
October 19, 2022 18:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you, so much :D !