How can I know whether a VPN connection is established or not?
Asked Answered
T

4

14

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.

Timer answered 1/9, 2012 at 12:4 Comment(0)
T
28

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!");
            }
        }
    }
}
Timer answered 1/9, 2012 at 16:45 Comment(4)
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable is the full module path.Brownedoff
this does not work for some vpns like NordVPN.Gwenni
What's the point of checking if NetworkInterfaceType == Ppp and afterwards checking if NetworkInterfaceType != Loopback?Burble
@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.Timer
L
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;
    }
Livelihood answered 17/8, 2017 at 17:47 Comment(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"Exodus
F
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);
    }
}
Fusain answered 28/9, 2019 at 1:42 Comment(0)
D
2

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;
                        }                           
                    }
                }
            }
Duggan answered 4/6, 2020 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.