RB.
RB.

Reputation: 37192

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.

Upvotes: 2

Views: 31383

Answers (4)

RB.
RB.

Reputation: 37192

Ok, this is a hacky solution I got to detect named VPNs. It will throw an error if it cannot connect to the VPN for whatever reason (including the network connection is down, the VPN does not exist, etc.).

Specific error codes to test for include :

Remote Access error 800 - Unable to establish the VPN connection.
The VPN server may be unreachable, or security parameters may not be configured properly for this connection.

Remote Access error 623 - The system could not find the phone book entry for this connection.

It doesn't really answer my question as posed (although it works well enough for the real-life problem).

Dim p As New Process

p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.FileName = "rasdial.exe"
p.StartInfo.Arguments = """Company HQ"""
p.Start()
If Not p.WaitForExit(My.Settings.VpnTimeout) Then
    Throw New Exception( _
String.Format("Connecting to ""{0}"" VPN failed after {1}ms", sVpn, My.Settings.VpnTimeout))
End If

If p.ExitCode <> 0 Then
    Throw New Exception( _
String.Format("Failed connecting to ""{0}"" with exit code {1}. Errors {2}", sVpn, p.ExitCode, p.StandardOutput.ReadToEnd.Replace(vbCrLf, "")))
End If

Upvotes: 2

Jeff Winn
Jeff Winn

Reputation:

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

Upvotes: 1

Paul Nearney
Paul Nearney

Reputation: 6955

OK - Last ditch effort :)

How about the Win32_NetworkConnection class - I'm pretty sure this will handle VPN connections

Upvotes: 0

Paul Nearney
Paul Nearney

Reputation: 6955

This example using WMI may get you most of the way (Sorry about the C# by the way!):

using System.Management;

string query = "SELECT * FROM Win32_NetworkAdapterConfiguration";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();

// Every record in this collection is a network interface
foreach (ManagementObject mo in moCollection)
{
    // Do what you need to here....
}

For testing whether the adapter is enabled or not, this link should help:

Win32_NetworkAdapterConfiguration class

Edit: You may have more luck with the Win32_NetworkAdapter class - it has Caption and Description properties which may give you what you need. There is a way to tie this info in with the previous class I mentioned, to get IP addresses etc - not sure what it is without further research, but let me know if you need to know.

Upvotes: 1

Related Questions