調用方法體 public void OpenBrowser() { string url = "http://localhost:8055/api"; bool openRes = OpenBrowserHelper.OpenChromeBrowserUrl(url); if (!openRes) ...
- 調用方法體
public void OpenBrowser() { string url = "http://localhost:8055/api"; bool openRes = OpenBrowserHelper.OpenChromeBrowserUrl(url); if (!openRes) openRes = OpenBrowserHelper.OpenDefaultBrowserUrl(url); if (!openRes) { // 打開下載鏈接,從官網下載 谷歌 if (MessageBox.Show("系統未安裝谷歌(Chrome)瀏覽器,是否下載安裝?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes) { OpenBrowserHelper.OpenIe("https://www.google.cn/chrome/"); } } }
- 打開瀏覽器幫助類
public class OpenBrowserHelper { /// <summary> /// 調用谷歌(Chrome)瀏覽器 /// </summary> /// <param name="url">打開網頁的鏈接</param> public static bool OpenChromeBrowserUrl(string url) { bool isOpen = true; try { // 谷歌瀏覽器就用谷歌打開,沒找到就用系統預設的瀏覽器 // 谷歌卸載了,註冊表還沒有清空,程式會返回一個"系統找不到指定的文件。"的bug var chromeKey = @"\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe"; // 通過註冊表找到谷歌瀏覽器安裝路徑 string chromeAppFileName = (string)(Registry.GetValue("HKEY_LOCAL_MACHINE" + chromeKey, "", null) ?? Registry.GetValue("HKEY_CURRENT_USER" + chromeKey, "", null)); // 找到谷歌瀏覽器則打開 if (!string.IsNullOrWhiteSpace(chromeAppFileName)) { Process.Start(chromeAppFileName, url); } else { isOpen = false; //預設瀏覽器打開 //OpenDefaultBrowserUrl(url); } } catch { isOpen = false; } return isOpen; } /// <summary> /// 調用IE瀏覽器 /// </summary> /// <param name="url"></param> public static void OpenIe(string url) { // IE瀏覽器路徑安裝:C:\Program Files\Internet Explorer // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo)註意這個錯誤 try { if (File.Exists(@"C:\Program Files\Internet Explorer\iexplore.exe")) { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:\Program Files\Internet Explorer\iexplore.exe", Arguments = url, UseShellExecute = false, CreateNoWindow = true }; Process.Start(processStartInfo); } else { if (File.Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe")) { ProcessStartInfo processStartInfo = new ProcessStartInfo { FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe", Arguments = url, UseShellExecute = false, CreateNoWindow = true }; Process.Start(processStartInfo); } } } catch (Exception exception) { } } /// <summary> /// 調用系統預設瀏覽器(用戶自己設置了預設瀏覽器) /// </summary> /// <param name="url"></param> public static bool OpenDefaultBrowserUrl(string url) { bool isOpen = true; try { //從註冊表中讀取預設瀏覽器可執行文件路徑 RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\"); if (key != null) { string browserPath = string.Empty; string[] splitArr = new string[] { }; string browser = key.GetValue("").ToString(); //browser是預設瀏覽器,不同的瀏覽器後面的參數不一樣。例如:"D:\Program Files (x86)\Google\chrome.exe" -- "%1" var lastIndex = browser.IndexOf(".exe", StringComparison.Ordinal); if (lastIndex == -1) lastIndex = browser.IndexOf(".EXE", StringComparison.Ordinal); if (lastIndex != -1) { splitArr = browser.Split("\""); } //大於0 說明 按照 " 切割到數據 if (splitArr.Length > 0) { browserPath = splitArr[1]; } else if (splitArr.Length == 0 && lastIndex != -1) //說明有瀏覽器,列如:D:\QQBrowser\QQBrowser.exe { browserPath = browser; } //打開瀏覽器 var result = Process.Start(browserPath, url); //調用IE //if (result == null) // OpenIe(url); } else { isOpen = false; //OpenIe(url); } } catch { isOpen = false; } return isOpen; } }