Solved

.NET - How to detect an internet connection

Posted on 2003-11-19
3
2,155 Views
Last Modified: 2013-11-25
In Visual Basic 5.0 and 6.0, you could make use of the "InternetCheckConnection" Win32 API located within the WININET.DLL library to detect if the user was connected to the internet.  The same API could be used to FORCE an internet connection (meaning if they aren't connected, tell their OS to connect).

Is there a way to do this within .NET?  I'm using VB.NET, but sample code in C++.NET, C#.NET, J#.NET, etc is ok to.  As long as it's CLR code I can understand it.

Thank You,

Kevin
0
Comment
Question by:HATCHET
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9781644
Are you looking for the functionality within the Framework?  AFAIK it is not in there.  You can still use the code from VB6 to check this.
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 150 total points
ID: 9782511
Public Function InternetConnectionExists2() As Boolean
    ' Determines if there is a connection to the Internet. This
    ' version uses an alternate approach to call the Windows API.
    ' Instead of directly testing for a connection, the code
    ' attempts to resolve to a URL that is assumed to be
    ' available. If the URL is not available, it returns false
    '

    ' Create the URI object
    Dim url As New System.Uri("http://www.microsoft.com")

    ' Create the web request object
    Dim WebReq As System.Net.WebRequest
    WebReq = System.Net.WebRequest.Create(url)

    Dim Resp As System.Net.WebResponse
    Try
        ' Try and get a response
        Resp = WebReq.GetResponse

        Resp.Close()
        WebReq = Nothing
        Return True

    Catch ex As Exception
        Resp.Close()
        WebReq = Nothing
        Return False

    End Try


End Function
==================================================
You also can try this

Imports System.Runtime.InteropServices

<DllImport("wininet.dll")> _
    Public Function InternetGetConnectedState( _
        ByVal lpdwFlags As Integer, ByVal res As Integer) As Boolean
End Function

Public Function InternetConnectionExists() As Boolean
    ' Determines if there is a connection to the Internet.
    ' Note that InternetGetConnectedState cannot be considered
    ' 100% reliable. An issue can occur if an internet connection
    ' has never been configured, the function may return true, even
    ' though there is no internet connection.
    '

    Dim e As Integer = 0
    Dim ret As Boolean = InternetGetConnectedState(e, 0)

    Return ret

End Function
0
 
LVL 3

Author Comment

by:HATCHET
ID: 9792182
iboutchkine,

Not the most eloquent way of doing it... but it does work.  =)

Thanks much!
Kevin
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question