英文:
How to make sure the PC is having internet and then launch exec?
问题
我有一个正在运行的应用程序,当Windows 8.1启动时,它会自动启动。但是经常出现电脑稍后连接到网络,结果Google Chrome显示加载失败的页面。
package main
import "os"
import "os/exec"
import "runtime"
import "encoding/json"
type Configuration struct {
main []string
name []string
window []string
}
func main() {
myos := runtime.GOOS;
myarch := runtime.GOARCH;
var chrome = "";
var cmdopen *exec.Cmd;
if myos == "windows" {
if myarch == "386" {
chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
} else {
chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
}
// 读取配置
file, _ := os.Open("C:/Program Files (x86)/abc/package.json");
decoder := json.NewDecoder(file);
configuration := Configuration{};
err := decoder.Decode(&configuration);
if err != nil {
println("error: ", err);
}
println(configuration.main);
// BUG!!!!!!!!!!!!!!!!!!! 但是请确保本地网络或互联网可用,不要像白痴一样直接执行Chrome,显示一个无效的页面
cmdopen = exec.Command(chrome, "--app=http://icanhazip.com");
err1 := cmdopen.Start();
if err1 != nil {
println("Failed: ", err1);
}
} else {
println("Incompatible");
}
}
以上是要翻译的内容。
英文:
I have this application running, when system boots Windows 8.1 then it launch. But often the PC get into the network later as a result Google Chrome shows a failed page.
package main
import "os"
import "os/exec"
import "runtime"
import "encoding/json"
type Configuration struct {
main []string
name []string
window []string
}
func main() {
myos := runtime.GOOS;
myarch := runtime.GOARCH;
var chrome = "";
var cmdopen *exec.Cmd;
if myos == "windows" {
if myarch == "386" {
chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
} else {
chrome = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
}
// Read config
file, _ := os.Open("C:/Program Files (x86)/abc/package.json");
decoder := json.NewDecoder(file);
configuration := Configuration{};
err := decoder.Decode(&configuration);
if err != nil {
println("error: ", err);
}
println(configuration.main);
// BUG!!!!!!!!!!!!!!!!!!! But make sure local network or internet is available do not just execute the chrome like idiot, which is showing dead page
cmdopen = exec.Command(chrome, "--app=http://icanhazip.com");
err1 := cmdopen.Start();
if err1 != nil {
println("Failed: ", err1);
}
} else {
println("Incompatible");
}
}
答案1
得分: 1
你可以使用http.Get()
函数来执行此操作。
func hazInternet() bool {
res, err := http.Get("http://www.google.com/robots.txt")
if err != nil {
log.Println(err)
return false
}
res.Body.Close()
return true
}
英文:
you could do an http.Get()
func hazInternet() bool {
res, err := http.Get("http://www.google.com/robots.txt")
if err != nil {
log.Println(err)
return false
}
res.Body.Close()
return true
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论