Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - How can I programmatically connect to a VPN?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...