我有 VPN 连接。为了建立 VPN 连接,必须执行一个 PPTP.bk 文件。运行此文件并输入凭据后,即会建立 VPN 连接。
我正在尝试以编程方式连接和断开 VPN 连接。 问题是 Windows 中没有创建 VPN 连接,所以我需要能够随时验证任何 VPN 连接,如果不存在,则建立一个。
最佳答案
我使用 NetworkInterface
类检查 VPN 连接状态。这是我为此目标编写的代码:
if (NetworkInterface.GetIsNetworkAvailable())
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface Interface in interfaces)
{
if (Interface.OperationalStatus == OperationalStatus.Up)
{
if ((Interface.NetworkInterfaceType == NetworkInterfaceType.Ppp) && (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback))
{
IPv4InterfaceStatistics statistics = Interface.GetIPv4Statistics();
MessageBox.Show(Interface.Name + " " + Interface.NetworkInterfaceType.ToString() + " " + Interface.Description);
}
else
{
MessageBox.Show("VPN Connection is lost!");
}
}
}
}
关于c# - 如何知道 VPN 连接是否建立?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227540/