Find and start a VPN connection in VB .NET

29,483

Solution 1

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

Solution 2

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.

Share:
29,483
Maurício
Author by

Maurício

I am a developer, primarily working on .NET applications and the Genesys Contact Centre Framework, as well as some Java apps when I am being punished. I have a BSc (Hons) in Computer Science, and have worked commercially in C, C#, Java, VB6, VB .NET, HTML, T-SQL, Javascript, VBScript, PowerShell, NSIS, and Batch (I love batch!!). I also have a keen interest in TFS, especially the build WorkFlow.

Updated on December 05, 2020

Comments

  • Maurício
    Maurício over 3 years

    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.