Solved

.NET - How to detect an internet connection

Posted on 2003-11-19
3
2,151 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
Comment Utility
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
Comment Utility
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
Comment Utility
iboutchkine,

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

Thanks much!
Kevin
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now