Menu

Enumerating Configured VPN Connections

January 29, 2017 - .NET, C#

I wrote previously about manners in which the SimpleWifi library can be utilized to enumerate available wireless connections and disconnect or connect to them. As my entire reason for writing BCNetMenu, however, was for VPN connections- not for wireless connections- it was necessary to figure out that piece of the puzzle as well.

The approach I discovered may not be entirely forward compatible, however it appears to be functional going back through many versions of Windows, so it ought to keep working. I wasn’t able to find a more official, “sanctioned” method. Thje basic idea is to effectively read the configuration information directly. The Configuration information for VPN connections can be found in the file “%APPDATA%\Microsoft\Network\Connections\Pbk\rasphone.pbk”. I suspect it may also be in the corresponding Common Application Data folder, found set in the %PROGRAMDATA% environment variable.

Retrieving the VPN names is fairly straight forward; effectively, we just want to find the section names. We can use a straight String parse of each line, but we can also use a Regular Expression with a group to define the actual name. Matches to:

is sufficient to find all the appropriate sections, and retrieve the names via the matches. However, the name is not quite enough; we need to cross-reference this information with information available via the NetworkInterface class; then we can use appropriate properties to return a particular data object representing the VPN connection:

But enumerating connections is one thing- connecting or disconnecting is another. After some searching I ended up finding only one method that was suitable for my use case, as other methods either required manual password input or to have BCNetMenu manage passwords, which I felt was outside the scope of what I wanted to do. Instead, the program will basically just run rasphone.exe with the appropriate VPN name; while this will show a dialog, the saved login information is pre-populated, so, at least for my intended use, I’ve found it sufficient to improve upon the default Windows 10 VPN Foldout.

For the same of comparison, here is the “before” image:

The default Windows 10 Network Foldout

This is an entirely usable foldout- or, it appears that way. However, clicking a connection takes one to the Control Panel. For example, here is the WIndow that appears when I click the connected “Mainframe” option:

From a UI design perspective this boggles my mind. There is zero indication that I clicked “Mainframe” at all. Why are these options listed separately and clickable individually if they all lead to the same place? Clicking a connected VPN connection should disconnect it; clicking a disconnected VPN connection should connect it. The way it has been altered in Windows 10 defies good UI design as far as I’m concerned.

Not that I’m any expert on good UI design; I just know what is easy to use for myself and when a “feature” or alteration causes one to occasionally mumble to themselves angrily or laugh about how silly the feature is even months after it’s introduction it probably wasn’t for the best. As far as getting the desired behaviour, I had two alternatives; the one that I originally used was a registry adjustment which would set the foldout to use the Windows 8 implementation. This worked for some time, however I found that, since the dialog hadn’t had adjustments for Windows 10, some features didn’t work properly; I found in some cases it wouldn’t respond to clicks or refused to connect to a wireless network, but the network control panel functioned as intended. In order to bring back my own desired behaviour, I created BCNetMenu, which appears like this:

BCNetMenu displaying available VPN connections.

It’s not the fanciest thing in the world; it’s not intended to blow anybody’s mind with an amazing glass-like appearance or transparent Window blur or anything like that. It’s a relatively basic pop-up menu that just lists available connections. Clicking a connected one disconnects. Clicking a disconnected one connects.

As it should be, if you ask me!

Have something to say about this post? Comment!