Winform calls the browser to open the page and share the method, hoping to help you. winform page

Source: Internet
Author: User

Winform calls the browser to open the page and share the method, hoping to help you. winform page

In many client programs, we need to call the browser to open the webpage. Here we share a method that can be used to call the browser in my winform program. The test is passed.

Download and view demo

View demo Diagram

1. Open the webpage by calling Google browser (Open Baidu)

2. Call IE to open the page (Open Baidu)

3. Call the default browser to open the Baidu page.

All tests are successful. Some computers fail to open IE because they have not installed IE, especially some Ghost systems. I suggest you call Google Chrome, because this is one of the most popular browsers, Google opens the default system if it does not, and IE if it does not.

In the project test, some customers could not open their computers with IE, and finally installed Google.

Additional source code demo:

Download and view demo

Finally, add the source code.

/// <Summary> /// call the system browser to open the webpage /// http://m.jb51.net/article/44622.htm /// </Summary> /// <param name = "url"> link to the webpage </param> public static void OpenBrowserUrl (string url) {try {// 64-bit registry path var openKey = @ "SOFTWARE \ Wow6432Node \ Google \ Chrome"; if (IntPtr. size = 4) {// 32-bit Registry path openKey = @ "SOFTWARE \ Google \ Chrome";} RegistryKey appPath = Registry. localMachine. openSubKey (openKey); // The Google browser is opened with Google. If it is not found, the default browser of the system is used. // Google is uninstalled, and the registry has not been cleared, the program returns a "the system cannot find the specified file. "Bug var openPath = appPath! = Null? "Chrome.exe": "EXPLORER. EXE "; Process. start (openPath, url);} catch {// An error occurred while calling the browser set by the user by default. If not, call IE opendefabrowbrowserurl (url );}} /// <summary> /// open the browser with IE /// </summary> /// <param name = "url"> </param> public static void OpenIe (string url) {try {Process. start ("iexplore.exe", url);} catch (Exception ex) {LogUtil. writeException (ex); // IE browser path installation: C: \ Program Files \ Internet Explorer // at System. di Agnostics. process. startWithshellExecuteEx (ProcessStartInfo startInfo) note this error 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);} else {if (MessageBox. show ("IE browser is not installed in the system. Do you want to download and install it? ", Null, MessageBoxButtons. YesNoCancel, MessageBoxIcon. Question) = DialogResult. Yes) {// open the download link and download OpenBrowserUrl (" http://windows.microsoft.com/zh-cn/internet-explorer/download-ie ") ;}}} Catch (Exception exception) {LogUtil. writeException (exception) ;}}/// <summary> // open the default browser of the system (you have set the default browser) /// </summary> /// <param name = "url"> </param> public static void opendefabrowbrowserurl (string url) {try {Process. start ("EXPLORER. EXE ", url) ;}catch {OpenIe (url );}}

If it is helpful to you, I hope you will like it and like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.