You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.3 KiB
64 lines
1.3 KiB
package initFunc |
|
|
|
import ( |
|
"errors" |
|
"io" |
|
"net/http" |
|
"os" |
|
"os/exec" |
|
) |
|
|
|
//检查是否安装WebView2,没有安装的话就安装 |
|
func CheckWebView2() error { |
|
cmd := exec.Command("cmd", "/C", "wmic product where \"name='Microsoft Edge WebView2 Runtime'\" get Name") |
|
output, err := cmd.Output() |
|
if err != nil { |
|
return err |
|
} |
|
// 检查输出是否包含 WebView2 Runtime 的名称 |
|
if string(output) == "Name\r\nMicrosoft Edge WebView2 Runtime\r\n" { |
|
return nil // WebView2 已安装 |
|
} else { |
|
err := runWebView2Installer() |
|
return err // WebView2 未安装 |
|
} |
|
} |
|
|
|
|
|
func runWebView2Installer() error { |
|
url := "https://suwa3d-website.oss-cn-shanghai.aliyuncs.com/package/WebView2.exe" |
|
filePath := "C:\\Program Files\\WebView2.exe" |
|
err := downloadFile(url, filePath) |
|
if err != nil { |
|
return errors.New("下载安装包失败-"+err.Error()) //fmt.Println("Download failed:", err) |
|
} |
|
cmd := exec.Command(filePath) |
|
err = cmd.Run() |
|
if err != nil { |
|
return errors.New("安装包安装失败-"+err.Error()) |
|
} |
|
return nil |
|
} |
|
|
|
|
|
|
|
func downloadFile(url string, filePath string) error { |
|
response, err := http.Get(url) |
|
if err != nil { |
|
return err |
|
} |
|
defer response.Body.Close() |
|
|
|
file, err := os.Create(filePath) |
|
if err != nil { |
|
return err |
|
} |
|
defer file.Close() |
|
|
|
_, err = io.Copy(file, response.Body) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
return nil |
|
} |