在很多客戶端程序中我們都需要調用瀏覽器打開網頁,這里分享一個可以在我winform程序調用瀏覽器的方法,測試通過了。
聲明:這個方法是上萬個用戶測試通過的,不是我沒有測試通過就拿出來分享,那個是自己搬起石頭砸自己的腳,還請大家自己下載demo測試一下。
看演示圖
1.調用谷歌瀏覽器打開網頁(打開百度)

2.調用IE打開頁面(打開百度)

3.調用用戶默認設置的瀏覽器打開百度頁面

測試都是通過的,有些電腦因為沒有安裝IE瀏覽器特別是一些Ghost系統,導致IE打開不成功,這里我建議大家可以調用谷歌瀏覽器,因為比較這是現在最主流的瀏覽器之一,谷歌沒有就打開系統默認的,實在不行就打開IE。
項目測試中,還真有一些客戶的電腦用IE打不開,最后安裝谷歌就可以了。
附加源碼程序demo:
最后我們加上源碼
1 using System; 2 using System.Diagnostics; 3 using System.IO; 4 using System.Windows.Forms; 5 using Microsoft.Win32; 6 7 namespace WindowsFormsApplication1 8 { 9 public class BrowserHelper 10 { 11 /// <summary> 12 /// 調用系統瀏覽器打開網頁 13 /// http://m.jb51.net/article/44622.htm 14 /// http://www.2cto.com/kf/201412/365633.html 15 /// </summary> 16 /// <param name="url">打開網頁的鏈接</param> 17 public static void OpenBrowserUrl(string url) 18 { 19 try 20 { 21 // 64位注冊表路徑 22 var openKey = @"SOFTWARE\Wow6432Node\Google\Chrome"; 23 if (IntPtr.Size == 4) 24 { 25 // 32位注冊表路徑 26 openKey = @"SOFTWARE\Google\Chrome"; 27 } 28 RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey); 29 // 谷歌瀏覽器就用谷歌打開,沒找到就用系統默認的瀏覽器 30 // 谷歌卸載了,注冊表還沒有清空,程序會返回一個"系統找不到指定的文件。"的bug 31 if (appPath != null) 32 { 33 var result = Process.Start("chrome.exe", url); 34 if (result == null) 35 { 36 OpenIe(url); 37 } 38 } 39 else 40 { 41 var result = Process.Start("chrome.exe", url); 42 if (result == null) 43 { 44 OpenDefaultBrowserUrl(url); 45 } 46 } 47 } 48 catch 49 { 50 // 出錯調用用戶默認設置的瀏覽器,還不行就調用IE 51 OpenDefaultBrowserUrl(url); 52 } 53 } 54 55 /// <summary> 56 /// 用IE打開瀏覽器 57 /// </summary> 58 /// <param name="url"></param> 59 public static void OpenIe(string url) 60 { 61 try 62 { 63 Process.Start("iexplore.exe", url); 64 } 65 catch (Exception ex) 66 { 67 MessageBox.Show(ex.Message); 68 // IE瀏覽器路徑安裝:C:\Program Files\Internet Explorer 69 // at System.Diagnostics.process.StartWithshellExecuteEx(ProcessStartInfo startInfo)注意這個錯誤 70 try 71 { 72 if (File.Exists(@"C:\Program Files\Internet Explorer\iexplore.exe")) 73 { 74 ProcessStartInfo processStartInfo = new ProcessStartInfo 75 { 76 FileName = @"C:\Program Files\Internet Explorer\iexplore.exe", 77 Arguments = url, 78 UseShellExecute = false, 79 CreateNoWindow = true 80 }; 81 Process.Start(processStartInfo); 82 } 83 else 84 { 85 if (File.Exists(@"C:\Program Files (x86)\Internet Explorer\iexplore.exe")) 86 { 87 ProcessStartInfo processStartInfo = new ProcessStartInfo 88 { 89 FileName = @"C:\Program Files (x86)\Internet Explorer\iexplore.exe", 90 Arguments = url, 91 UseShellExecute = false, 92 CreateNoWindow = true 93 }; 94 Process.Start(processStartInfo); 95 } 96 else 97 { 98 if (MessageBox.Show(@"系統未安裝IE瀏覽器,是否下載安裝?", null, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes) 99 { 100 // 打開下載鏈接,從微軟官網下載 101 OpenDefaultBrowserUrl("http://windows.microsoft.com/zh-cn/internet-explorer/download-ie"); 102 } 103 } 104 } 105 } 106 catch (Exception exception) 107 { 108 MessageBox.Show(exception.Message); 109 } 110 } 111 } 112 113 /// <summary> 114 /// 打開系統默認瀏覽器(用戶自己設置了默認瀏覽器) 115 /// </summary> 116 /// <param name="url"></param> 117 public static void OpenDefaultBrowserUrl(string url) 118 { 119 try 120 { 121 // 方法1 122 //從注冊表中讀取默認瀏覽器可執行文件路徑 123 RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\"); 124 if (key != null) 125 { 126 string s = key.GetValue("").ToString(); 127 //s就是你的默認瀏覽器,不過后面帶了參數,把它截去,不過需要注意的是:不同的瀏覽器后面的參數不一樣! 128 //"D:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -- "%1" 129 var lastIndex = s.IndexOf(".exe", StringComparison.Ordinal); 130 if (lastIndex == -1) 131 { 132 lastIndex = s.IndexOf(".EXE", StringComparison.Ordinal); 133 } 134 var path = s.Substring(1, lastIndex + 3); 135 var result = Process.Start(path, url); 136 if (result == null) 137 { 138 // 方法2 139 // 調用系統默認的瀏覽器 140 var result1 = Process.Start("explorer.exe", url); 141 if (result1 == null) 142 { 143 // 方法3 144 Process.Start(url); 145 } 146 } 147 } 148 else 149 { 150 // 方法2 151 // 調用系統默認的瀏覽器 152 var result1 = Process.Start("explorer.exe", url); 153 if (result1 == null) 154 { 155 // 方法3 156 Process.Start(url); 157 } 158 } 159 } 160 catch 161 { 162 OpenIe(url); 163 } 164 } 165 166 /// <summary> 167 /// 火狐瀏覽器打開網頁 168 /// </summary> 169 /// <param name="url"></param> 170 public static void OpenFireFox(string url) 171 { 172 try 173 { 174 // 64位注冊表路徑 175 var openKey = @"SOFTWARE\Wow6432Node\Mozilla\Mozilla Firefox"; 176 if (IntPtr.Size == 4) 177 { 178 // 32位注冊表路徑 179 openKey = @"SOFTWARE\Mozilla\Mozilla Firefox"; 180 } 181 RegistryKey appPath = Registry.LocalMachine.OpenSubKey(openKey); 182 if (appPath != null) 183 { 184 var result = Process.Start("firefox.exe", url); 185 if (result == null) 186 { 187 OpenIe(url); 188 } 189 } 190 else 191 { 192 var result = Process.Start("firefox.exe", url); 193 if (result == null) 194 { 195 OpenDefaultBrowserUrl(url); 196 } 197 } 198 } 199 catch 200 { 201 OpenDefaultBrowserUrl(url); 202 } 203 } 204 } 205 }
如果對你有幫助希望你可以喜歡,點個贊。
