Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.NET - How to detect an internet connection

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

604 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