Solved

.NET - How to detect an internet connection

Posted on 2003-11-19
3
2,163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

726 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