views:

2804

answers:

4

Hi all,

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.

+1  A: 

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.

Paul Nearney
Hi Paul. The "NetConnectionID" will show the name (e.g. "Local Area Connection") for my NIC, but won't show this for my VPN, even if it is connected. Thanks for your help though.
RB
+1  A: 

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 = """Girlings 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
RB
A: 

OK - Last ditch effort :)

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

Paul Nearney
On my machine this shows all my mapped drives, and any network shares I have open. It doesn't show VPNs, even if they're connected :-( Thanks for your help though :-)
RB
Bugger - the info you need must be in WMI somewhere - just a case of wading through the documentation to find the right class :o( Glad you found a workaround
Paul Nearney
+1  A: 
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

Hi Jeff, that is *exactly* what I was looking for. I'm going to download the source later to find out how you've programmed such magical wizardry! Many, many thanks :-)
RB