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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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?
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.