13

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224

4 Answers4

27

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!");
            }
        }
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Hossein
  • 24,202
  • 35
  • 119
  • 224
  • 2
    `System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable` is the full module path. – Mick Dec 06 '16 at 20:59
  • this does not work for some vpns like NordVPN. – smedasn May 16 '21 at 12:01
  • What's the point of checking if NetworkInterfaceType == Ppp and afterwards checking if NetworkInterfaceType != Loopback? – brz Oct 25 '22 at 05:44
  • @breez, good point. I have no idea! I guess I might have scrapped this out of a larger code base of mine, which I ultimately failed to do properly, thus leaving that portion. – Hossein Oct 25 '22 at 06:11
10

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;
    }
Mike
  • 620
  • 7
  • 21
  • 1
    to add, cmd `ipconfig /all` on Windows to show the description of your adapter(s), then can change the "TAP-Windows Adapter" string to your VPN of choice e.g. "PANGP Virtual Ethernet Adapter" – Slate Apr 13 '21 at 10:09
5

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);
    }
}
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
0

For me I found this helpful, but I wanted to check for a specific VPN name after looping over all the interfaces and upon finding the one that is UP...

            if (NetworkInterface.GetIsNetworkAvailable())
            {
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface Interface in interfaces)
                {
                    if (Interface.OperationalStatus == OperationalStatus.Up)
                    {
                        if (Interface.Description.Contains("VPN_Name"))
                        {
                            connected = true;
                            break;
                        }                           
                    }
                }
            }
Tom Stickel
  • 19,633
  • 6
  • 111
  • 113