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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:46:19  来源:igfitidea点击:

Find and start a VPN connection in VB .NET

.netvb.netwindows-xpnetwork-programming

提问by RB.

I am using NetworkInterface.GetAllNetworkInterfaces()to get all the interfaces on a PC. However, this appears to only return "active" interfaces. How can I find "inactive" network interfaces, such as unconnected VPNs, disabled NICs, etc. in .NET.

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

I would like to find them by their name in "Control Panel" -> "Network Connections". So, for example, if I have a VPN called "My Work" I would like to be able to find it using the name "My Work".

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

Using Win32_NetworkAdapterConfiguration does not seem to be an option as it does not return the name shown in "Network Connections" (as far as I can see).

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

Many thanks,

非常感谢,

RB.

RB。

采纳答案by RB.

RasDialer dialer = new RasDialer();
ReadOnlyCollection<RasConnection> connections = dialer.GetActiveConnections();

foreach (RasConnection connection in connections)
{
    // Do what you want to with the connections.
}

That will retrieve all connected dial up entries (including VPN connections) that are in use by Windows along with their related handles and other information.

这将检索 Windows 使用的所有已连接拨号条目(包括 VPN 连接)及其相关句柄和其他信息。

That component is part of the DotRas project on CodePlex.

该组件是 CodePlex 上的 DotRas 项目的一部分。

http://www.codeplex.com/DotRas

http://www.codeplex.com/DotRas

回答by Paul Nearney

This example using WMI may get you most of the way (Sorry about the C# by the way!):

这个使用 WMI 的例子可能会让你大获全胜(顺便说一句,对不起 C#!):

using System.Management;

string query = "SELECT * FROM Win32_NetworkAdapterConfiguration";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();

// Every record in this collection is a network interface
foreach (ManagementObject mo in moCollection)
{
    // Do what you need to here....
}

For testing whether the adapter is enabled or not, this link should help:

要测试适配器是否已启用,此链接应该会有所帮助:

Win32_NetworkAdapterConfiguration class

Win32_NetworkAdapterConfiguration 类

Edit: You may have more luck with the Win32_NetworkAdapterclass - it has Caption and Description properties which maygive you what you need. There is a way to tie this info in with the previous class I mentioned, to get IP addresses etc - not sure what it is without further research, but let me know if you need to know.

编辑:您可能对Win32_NetworkAdapter类有更多的运气- 它具有 Caption 和 Description 属性,可以为您提供所需的内容。有一种方法可以将此信息与我提到的上一堂课联系起来,以获取 IP 地址等 - 不确定它是什么,无需进一步研究,但如果您需要知道,请告诉我。

回答by RB.

Ok, this is a hacky solution I got to detect named VPNs. It will throw an error if it cannot connect to the VPN for whatever reason (including the network connection is down, the VPN does not exist, etc.).

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

Specific error codes to test for include :

要测试的特定错误代码包括:

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.

远程访问错误 800 - 无法建立 VPN 连接。
VPN 服务器可能无法访问,或者可能没有为此连接正确配置安全参数。

远程访问错误 623 - 系统找不到此连接的电话簿条目。

It doesn't really answer my question as posed (although it works well enough for the real-life problem).

它并没有真正回答我提出的问题(尽管它对于现实生活中的问题已经足够好了)。

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

回答by Paul Nearney

OK - Last ditch effort :)

好的 - 最后的努力:)

How about the Win32_NetworkConnectionclass - I'm pretty sure this will handle VPN connections

Win32_NetworkConnection类怎么样- 我很确定这将处理 VPN 连接