vb.net – 检查Internet连接

vb.net – 检查Internet连接,第1张

概述我需要我的应用程序来检查用户计算机上的互联网连接.如果有,则显示图像,如果没有,则显示不同的图像.这是我用来实现这个的代码: Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded If Network 我需要我的应用程序来检查用户计算机上的互联网连接.如果有,则显示图像,如果没有,则显示不同的图像.这是我用来实现这个的代码:

Private Sub Window_Loaded(ByVal sender As System.Object,ByVal e As System.windows.RoutedEventArgs) Handles MyBase.Loaded    If networkinformation.NetworkInterface.GetIsNetworkAvailable Then        Dim bi1 As New BitmAPImage        bi1.BeginInit()        bi1.UriSource = New Uri("Images\greenbar.png",UriKind.relative)        bi1.EndInit()        Image2.source = bi1    Else        Dim bi2 As New BitmAPImage        bi2.BeginInit()        bi2.UriSource = New Uri("Images\redbar.png",UriKind.relative)        bi2.EndInit()        Image2.source = bi2        MessageBox.Show("INTERNET CONNECTION NOT DETECTED")        MessageBox.Show("You must be connected to the internet to use some aspects of this application.")        MessageBox.Show("Please re-establish connection to the Internet and try again,thank you.")        Me.Close()    End IfEnd Sub

我决定通过更改我的默认网关在我自己的计算机上测试它(从而使它看起来好像我失去了连接).但我意识到代码仍显示我已连接.所以我认为它只是检查接口的连接 – 在这种情况下,是我与路由器的连接(这是真的,我连接到路由器).

所以,问题是:如何检查用户的PC是否实际连接到互联网?我阅读了文章What is the best way to check for Internet connectivity using .NET?,但它是在C#中,我不明白.

解决方法 您可以使用 this tool将C#转换为VB.NET,反之亦然:

Public Shared Function CheckForInternetConnection() As Boolean    Try        Using clIEnt = New WebClIEnt()            Using stream = clIEnt.OpenRead("http://www.Google.com")                Return True            End Using        End Using    Catch        Return False    End TryEnd Function

顺便说一下,您使用的NetworkInterface.GetIsNetworkAvailable方法检查是否有任何网络连接可用 – 而不是Internet连接.

A network connection is consIDered to be available if any network interface is marked “up” and is not a loopback or tunnel interface.

总结

以上是内存溢出为你收集整理的vb.net – 检查Internet连接全部内容,希望文章能够帮你解决vb.net – 检查Internet连接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1245031.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存