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

Rika picture Rika · Sep 1, 2012 · Viewed 18.1k times · Source

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.

Answer

Rika picture Rika · Sep 1, 2012

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!");
            }
        }
    }
}