好的,这是我用来检测命名 VPN 的一个 hacky 解决方案。如果由于某种原因(包括网络连接断开、VPN 不存在等)无法连接到 VPN,它会抛出错误。
要测试的特定错误代码包括:
远程访问错误 800 - 无法建立 VPN 连接。
VPN 服务器可能无法访问,或者可能没有为此连接正确配置安全参数。
远程访问错误 623 - 系统找不到此连接的电话簿条目。
它并没有真正回答我提出的问题(尽管它对于现实生活中的问题来说已经足够好了)。
Dim p As New Process
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.FileName = "rasdial.exe"
p.StartInfo.Arguments = """Company HQ"""
p.Start()
If Not p.WaitForExit(My.Settings.VpnTimeout) Then
Throw New Exception( _
String.Format("Connecting to ""{0}"" VPN failed after {1}ms", sVpn, My.Settings.VpnTimeout))
End If
If p.ExitCode <> 0 Then
Throw New Exception( _
String.Format("Failed connecting to ""{0}"" with exit code {1}. Errors {2}", sVpn, p.ExitCode, p.StandardOutput.ReadToEnd.Replace(vbCrLf, "")))
End If