I have a VPN connection that I keep losing, that I need to connect to our DB server, but every second or third connection fails because I have lost the VPN connection. I'd like to add somde code - for DEBUG config only - to check the VPN connection and reconnect if necessary, before proceeding to attempt the database connection.
Asked
Active
Viewed 2.3k times
1 Answers
18
You could use System.Net.NetworkInformation.Ping to check if the connection is up - then rasdial to reconnect the vpn if the connection is lost.
eg
System.Diagnostics.Process.Start("rasdial.exe", "VPNName Username Password");
You can also disconnect the VPN using
System.Diagnostics.Process.Start("rasdial.exe", "VPNName /d");
Update
Calling rasdial with no arguments returns a list of open connections - might be a more robust solution for you.

PaulB
- 23,264
- 14
- 56
- 75
-
Great stuff for rasdial, but now what do I ping to see if the network is up? I can ping any server on the VPN, but if that server is down, it will appear to my AutoVpn code that I have lost the VPN connection. – ProfK Aug 16 '11 at 16:54
-
I think that will do @Paul, as that is the server I keep missing. Thanks. – ProfK Aug 17 '11 at 07:09