vb.net 在默认浏览器中打开网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6613239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:35:26  来源:igfitidea点击:

Open a webpage in the default browser

vb.netbrowser

提问by Freesn?w

I want my users to be able to click a button to open my company's webpage in the default browser when clicked. How would I do this?

我希望我的用户能够点击一个按钮,在点击时在默认浏览器中打开我公司的网页。我该怎么做?

I'm using VB.net so all .net examples are acceptable.

我正在使用 VB.net,因此所有 .net 示例都是可以接受的。

采纳答案by Joel Coehoorn

As others have indicated, Process.Start()is the way to go here. However, there are a few quirks. It's worth your time to read this blog post:

正如其他人所指出的那样,Process.Start()是去这里的方式。但是,有一些怪癖。值得您花时间阅读这篇博文:

http://faithlife.codes/blog/2008/01/using_processstart_to_link_to/

http://faithlife.codes/blog/2008/01/using_processstart_to_link_to/

In summary, some browsers cause it to throw an exception for no good reason, the function can block for a while on non-UI thread so you need to make sure it happens near the end of whatever other actions you might perform at the same time, and you might want to change the cursor appearance while waiting for the browser to open.

总之,某些浏览器会无缘无故地导致它抛出异常,该函数可能会在非 UI 线程上阻塞一段时间,因此您需要确保它在您可能同时执行的任何其他操作结束时发生,并且您可能希望在等待浏览器打开时更改光标外观。

回答by

This should work:

这应该有效:

Dim webAddress As String = "http://www.example.com/"
Process.Start(webAddress)

回答by keyboardP

You can use Process.Start:

您可以使用Process.Start

Dim url As String = “http://www.example.com“

Process.Start(url)

This should open whichever browser is set as default on the system.

这应该打开系统上设置为默认的浏览器。

回答by STiTCHiCKED

Here is a little sub that may just interest some people who need to specify the browser. (but its not as good as a 12" pizza sub!) :P

这是一个小子,可能会让一些需要指定浏览器的人感兴趣。(但它不如 12 英寸的比萨饼好!):P

Private Sub NavigateWebURL(ByVal URL As String, Optional browser As String = "default")

    If Not (browser = "default") Then
        Try
            '// try set browser if there was an error (browser not installed)
            Process.Start(browser, URL)
        Catch ex As Exception
            '// use default browser
            Process.Start(URL)
        End Try

    Else
        '// use default browser
        Process.Start(URL)

    End If

End Sub


Call: will open www.google.com in Firefox if it is installed on that PC.

调用:如果安装在该 PC 上,将在 Firefox 中打开 www.google.com。

NavigateWebURL("http://www.google.com", "Firefox") '// safari Firefox chrome etc


Call: will open www.google.com in default browser.

调用:将在默认浏览器中打开 www.google.com。

NavigateWebURL("http://www.google.com", "default")

OR

或者

NavigateWebURL("http://www.google.com")

回答by Kevin Nagel

This worked perfectly for me. Since this is for personal use, I used Firefox as my browser.

这对我来说非常有效。由于这是供个人使用,我使用 Firefox 作为我的浏览器。

 Dim url As String
    url = "http://www.google.com"
    Process.Start("Firefox", url)

回答by User021

Dim URL As String 
Dim browser As String = TextBox1.Text
URL = TextBox1.Text
Try
    If Not (browser = TextBox1.Text) Then
        Try
            Process.Start(browser, URL)
        Catch ex As Exception
            Process.Start(URL)
        End Try
    Else
        Process.Start(URL)
    End If

Catch ex As Exception
    MsgBox("There's something wrong!")
End Try

回答by R.Alonso

System.Diagnostics.Process.Start("http://www.example.com")

System.Diagnostics.Process.Start(" http://www.example.com")

回答by Jimboy

or sometimes it's very easy just type Process.Start("http://www.example.com/")

或者有时输入 Process.Start(" http://www.example.com/")很简单

then change the http://www.example.com/")

然后更改http://www.example.com/")