Search code examples
.netvb.netwindows-xpnetwork-programming

Find and start a VPN connection in VB .NET


I am using NetworkInterface.GetAllNetworkInterfaces() to get all the interfaces on a PC. However, this appears to only return "active" interfaces. How can I find "inactive" network interfaces, such as unconnected VPNs, disabled NICs, etc. in .NET.

I would like to find them by their name in "Control Panel" -> "Network Connections". So, for example, if I have a VPN called "My Work" I would like to be able to find it using the name "My Work".

Using Win32_NetworkAdapterConfiguration does not seem to be an option as it does not return the name shown in "Network Connections" (as far as I can see).

Many thanks,

RB.


Solution

  • RasDialer dialer = new RasDialer();
    ReadOnlyCollection<RasConnection> connections = dialer.GetActiveConnections();
    
    foreach (RasConnection connection in connections)
    {
        // Do what you want to with the connections.
    }
    

    That will retrieve all connected dial up entries (including VPN connections) that are in use by Windows along with their related handles and other information.

    That component is part of the DotRas project on CodePlex.

    http://www.codeplex.com/DotRas