?
Solved

.NET - How to detect an internet connection

Posted on 2003-11-19
3
Medium Priority
?
2,165 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 600 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 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