.net - 在 VB .NET 中查找并启动 VPN 连接

标签 .net vb.net windows-xp network-programming

我正在使用NetworkInterface.GetAllNetworkInterfaces()获取PC上的所有接口(interface)。然而,这似乎只返回“事件”接口(interface)。如何在 .NET 中找到“不活动”的网络接口(interface),例如未连接的 VPN、禁用的 NIC 等。

我想在“控制面板”->“网络连接”中按名称找到它们。因此,举例来说,如果我有一个名为“My Work”的 VPN,我希望能够使用名称“My Work”找到它。

使用 Win32_NetworkAdapterConfiguration 似乎不是一个选项,因为它不会返回“网络连接”中显示的名称(据我所知)。

非常感谢,

RB。

最佳答案

好吧,这是一个我用来检测命名 VPN 的黑客解决方案。如果由于任何原因(包括网络连接断开、VPN 不存在等)无法连接到 VPN,则会抛出错误。

要测试的具体错误代码包括:

Remote Access error 800 - Unable to establish the VPN connection.
The VPN server may be unreachable, or security parameters may not be configured properly for this connection.

Remote Access error 623 - The system could not find the phone book entry for this connection.

它并没有真正回答我提出的问题(尽管它足以解决现实生活中的问题)。

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

关于.net - 在 VB .NET 中查找并启动 VPN 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/349729/

相关文章:

c# - 一个类轮 : WeakReference-to-a-Lambda Event Handler

c# - 我可以制作一个模板,或者在 C# 中生成一堆几乎相同的属性吗?

.net - 从 t4 预处理文本模板生成多个输出

javascript - 包括特定于 ASP.NET MVC4 View 或部分 View 的脚本

.net - 从开发角度 : How do I convice the bosses to upgrade to Vista/Windows 7?

python - 是否可以从 Python 中杀死 Windows 上的进程?

.net - 微软自己的面向文档的数据库版本?

c# - 正则表达式 : .net 与 javascript 中特殊字符的差异

.net - 将 VB6 容器类升级到 VB.NET

c# - 使用 Process.Start() 启动时安装程序失败,但双击时可以运行