How can I programmatically connect to a VPN?

20,523

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.

Share:
20,523
ProfK
Author by

ProfK

I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET, with SQL Server. I have, in some way or another, been involved in software development for about eighteen years, but always learning something new. At the moment that is WPF and MVVM.

Updated on July 09, 2022

Comments

  • ProfK
    ProfK almost 2 years

    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.