How can I programmatically connect to a VPN?

你说的曾经没有我的故事 提交于 2019-12-18 04:42:09

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7076711/how-can-i-programmatically-connect-to-a-vpn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!