1. 程式人生 > 實用技巧 >winform中開啟谷歌瀏覽器

winform中開啟谷歌瀏覽器

公司的winform專案中需要在點選選單時用谷歌瀏覽器開啟網頁,找了很久看到一個博主發的,親測可用,記錄一下備用。

原文:https://www.cnblogs.com/wohexiaocai/p/4522046.html

     public static void OpenBrowserUrl(string url)
        {
            try
            {
                // 64位登錄檔路徑
                var openKey = @"SOFTWARE\Wow6432Node\Google\Chrome";
                
if (IntPtr.Size == 4) { // 32位登錄檔路徑 openKey = @"SOFTWARE\Google\Chrome"; } RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey); // 谷歌瀏覽器就用谷歌開啟,沒找到就用系統預設的瀏覽器 // 谷歌解除安裝了,登錄檔還沒有清空,程式會返回一個"系統找不到指定的檔案。"的bug
if (appPath != null) { var result = Process.Start("chrome.exe", url); if (result == null) { OpenIe(url); } } else {
var result = Process.Start("chrome.exe", url); if (result == null) { OpenIe(url); } } } catch { //出錯呼叫IE OpenIe(url); } } /// <summary> /// 用IE開啟瀏覽器 /// </summary> /// <param name="url"></param> public static void OpenIe(string url) { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = "iexplore.exe"; //IE瀏覽器,可以更換 process.StartInfo.Arguments = url; process.Start(); }