在很多客户端程序中我们都需要调用浏览器打开网页,这里分享一个可以在我winform程序调用浏览器的方法,测试通过了。

声明:这个方法是上万个用户测试通过的,不是我没有测试通过就拿出来分享,那个是自己搬起石头砸自己的脚,还请大家自己下载demo测试一下。

下载查看demo

看演示图

1.调用谷歌浏览器打开网页(打开百度)

C# winform调用浏览器打开页面方法分享,希望对大家有帮助

2.调用IE打开页面(打开百度)

C# winform调用浏览器打开页面方法分享,希望对大家有帮助

3.调用用户默认设置的浏览器打开百度页面

C# winform调用浏览器打开页面方法分享,希望对大家有帮助

测试都是通过的,有些电脑因为没有安装IE浏览器特别是一些Ghost系统,导致IE打开不成功,这里我建议大家可以调用谷歌浏览器,因为比较这是现在最主流的浏览器之一,谷歌没有就打开系统默认的,实在不行就打开IE。

项目测试中,还真有一些客户的电脑用IE打不开,最后安装谷歌就可以了。

附加源码程序demo:

下载查看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 }

 

如果对你有帮助希望你可以喜欢,点个赞。

分类:

技术点:

相关文章: