Watch for a dialup connection being disconnected

I need to run a command as soon as a particular dialup connection has been disconnected.

I'm using VB .NET in the Visual Studio 2008 package.

using the command line i can use the RASDIAL command and it will tell me what connections are active, so I am assuming that it should be possible to find out through some code if a particular connection is active.

Any Ideas?


Who is Participating?
Andy1Author Commented:
ok, i think i've found a solution...

When disconnected, the dialup account does'nt appear as a network interface. So I run a check on the interfaces and search for the interface name I am looking for, if it exists, im connected, if it doesn't then its no longer connected.

I found a sample and built this function from it:

 Public Function FindDialupInterface(ByVal name As String) As Boolean
        Dim computerProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
        Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        If nics Is Nothing OrElse nics.Length < 1 Then
            Return False
            Exit Function
        End If
        For Each adapter As NetworkInterface In nics
            If adapter.Name = name Then
                Return True
            End If
    End Function

To check for a network connection in .NET 2.0 use this:

To monitor a change in IP address or a change in network availability, use the events from the NetworkChange class:


Function HasConnectivity() As Boolean
        Dim hostInfo As System.Net.IPHostEntry
        Try 'to look for the logon server
            Dim sServer As String = Environment.GetEnvironmentVariable("logonserver")
            hostInfo = System.Net.Dns.GetHostByName(sServer.Remove(0, 2))
            Return True
        Catch 'theres no network connection
            Return False
        End Try
    End Function
Andy1Author Commented:
It keeps returning back false when im connected or disconnected.

I assume that "logonserver" is meant to be the name of my dialup connection?
Andy1Author Commented:
oh, ignore that last comment of mine.

I tried changing logonserver to the name of my dialup connection because I need it to happen when a specific connection is closed.

I changed it back, and it tells me that the connection is on, even when disconnected.

I am assuming that this is because the pc is connected on a network and its not looking specifically at the dialup connection?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.