Kaynağa Gözat

GetChromeExePath

ila 3 yıl önce
ebeveyn
işleme
cbb006555b
3 değiştirilmiş dosya ile 97 ekleme ve 4 silme
  1. 43 4
      core/chrome.go
  2. 45 0
      core/file.go
  3. 9 0
      main.go

+ 43 - 4
core/chrome.go

@@ -3,32 +3,71 @@ package core
 import (
 	"fmt"
 	"golang.org/x/sys/windows/registry"
+	"os"
 	"path/filepath"
 	"strconv"
 	"strings"
 )
 
+func GetChromeExePath() (path string, err error) {
+	key, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\\", registry.READ)
+
+	if err != nil {
+		XWarning(fmt.Sprintf("GetChromeExePath OpenKey  error : %v\n", err))
+		return
+	}
+	defer key.Close()
+
+	path, _, err = key.GetStringValue("Path")
+	if err != nil {
+		XWarning(fmt.Sprintf("GetChromeExePath GetStringValue  error : %v\n", err))
+		return
+	}
+	if path[len(path)-len("chrome.exe"):] != "chrome.exe" {
+		path = filepath.Join(path, "chrome.exe")
+	}
+
+	return
+}
+
 // @title    GetChromeUserDataDir
 // @description    获取windows当前用户的chrome用户目录
 // @param
 // @return chromeuserDataDir string
 func GetChromeUserDataDir() string {
-	return filepath.Join("C:/Users", GetWinUserName(), "AppData", "Local", "Google", "Chrome", "User Data")
+	userHome, _ := os.UserHomeDir()
+	return filepath.Join(userHome, "/AppData/Local/Google/Chrome/User Data/Default")
+}
+
+//CheckChromeInstalled 检查是否安装了chrome
+func CheckChromeInstalled() bool {
+	key, err := registry.OpenKey(registry.CURRENT_USER, "Software\\Google\\Chrome", registry.READ)
+
+	if err != nil {
+		XWarning(fmt.Sprintf("CheckChromeInstalled OpenKey  error : %v\n", err))
+		return false
+	}
+	if key != 0 {
+		return true
+	}
+	//fmt.Printf("key=====>%v\n", key)
+	return false
 }
 
+// GetChromeVersionFromRegedit 从注册表获取chrome的版本
 func GetChromeVersionFromRegedit() int {
 	ChromeVersion := 90
-	key, err := registry.OpenKey(registry.CURRENT_USER, "Software\\Google\\Chrome\\BLBeacon", registry.ALL_ACCESS)
+	key, err := registry.OpenKey(registry.CURRENT_USER, "Software\\Google\\Chrome\\BLBeacon", registry.READ)
 
 	if err != nil {
-		LogWrite(fmt.Sprintf("GetChromeVersionFromRegedit OpenKey  error : %v\n", err))
+		XWarning(fmt.Sprintf("GetChromeVersionFromRegedit OpenKey  error : %v\n", err))
 		return ChromeVersion
 	}
 	defer key.Close()
 
 	version, _, err := key.GetStringValue("version")
 	if err != nil {
-		LogWrite(fmt.Sprintf("GetChromeVersionFromRegedit OpenKey  error : %v\n", err))
+		XWarning(fmt.Sprintf("GetChromeVersionFromRegedit OpenKey  error : %v\n", err))
 		return ChromeVersion
 	}
 

+ 45 - 0
core/file.go

@@ -1,13 +1,58 @@
 package core
 
 import (
+	"fmt"
 	"io/ioutil"
 	"log"
 	"os"
 	"path/filepath"
 	"sort"
+	"strings"
 )
 
+//GetAllFilePathByExts 从指定路径获取指定后后缀的文件列表,ext:.go
+func GetAllFilePathByExts(currentPath string, exts []string) (filteredPaths []string, err error) {
+
+	Paths, err := GetAllFilePath([]string{}, currentPath)
+	if err != nil {
+		XWarning(fmt.Sprintf("GetAllFilePathByExt error : %v\n", err))
+		return
+	}
+
+	// 过滤是否包含指定后缀
+	for _, path := range Paths {
+		for _, ext := range exts {
+			if strings.HasSuffix(path, ext) {
+				filteredPaths = append(filteredPaths, path)
+			}
+		}
+
+	}
+
+	return
+}
+
+//GetAllFilePathByExt 从指定路径获取指定后后缀的文件列表,ext:.go
+func GetAllFilePathByExt(currentPath string, ext string) (filteredPaths []string, err error) {
+
+	Paths, err := GetAllFilePath([]string{}, currentPath)
+	if err != nil {
+		XWarning(fmt.Sprintf("GetAllFilePathByExt error : %v\n", err))
+		return
+	}
+
+	// 过滤是否包含指定后缀
+	for _, path := range Paths {
+
+		if strings.HasSuffix(path, ext) {
+			filteredPaths = append(filteredPaths, path)
+		}
+
+	}
+
+	return
+}
+
 // GetAllFilePath 获取指定目录内的文件和子目录的文件,递归获取
 func GetAllFilePath(filePaths []string, currentPath string) ([]string, error) {
 	//2021-12-23

+ 9 - 0
main.go

@@ -1,5 +1,10 @@
 package main
 
+import (
+	"fmt"
+	"gcore/core"
+)
+
 func init() {
 	//devConf := &xnet.DevConf{
 	//	AppKey:             "ding8svryas6vm6ze5xh",
@@ -21,6 +26,10 @@ func init() {
 	//fmt.Printf("%v\n", deptUserList)
 	//workData,_:=core.GetWorkData(30)
 	//fmt.Printf("%v\n", workData)
+
+	ret := core.GetChromeUserDataDir()
+	fmt.Printf("ret=>%v\n", ret)
+
 }
 
 func main() {