I have a VPN connection. In order to establish the VPN connection, there is a PPTP.bk file which must be executed. Upon running this file and entering the credentials, the VPN connection is established.
I am trying to connect and disconnect the VPN connection programmatically. The catch is there is no VPN connection created in the Windows so I need to be able to verify any VPN connection at any time and if it is not present establish one.
A VPN is created by establishing a virtual point-to-point connection through the use of dedicated circuits or with tunneling protocols over existing networks. A VPN available from the public Internet can provide some of the benefits of a wide area network (WAN).
A slight modification - this is the code that worked for me.
public bool CheckForVPNInterface()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
NetworkInterface[] interfaces =
NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface Interface in interfaces)
{
// This is the OpenVPN driver for windows.
if (Interface.Description.Contains("TAP-Windows Adapter")
&& Interface.OperationalStatus == OperationalStatus.Up)
{
return true;
}
}
}
return false;
}
I check the VPN connection status using the NetworkInterface
class. Here is the code I wrote for this goal:
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!");
}
}
}
}
On my network driver there was Cisco
in the description text. Here is a more up-todate version which is only a few lines:
public static class VPNCheck
{
public static bool IsOn()
{
return ((NetworkInterface.GetIsNetworkAvailable())
&& NetworkInterface.GetAllNetworkInterfaces()
.FirstOrDefault(ni => ni.Description.Contains("Cisco"))?.OperationalStatus == OperationalStatus.Up);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With