Due to work related reasons, I’ve been having less time for complex pet projects like coding a Gameboy emulator, detecting Stars Wars objects using AI or solving Rubik’s cube using genetic algorithms, so this month my post will be fairly simple and straightforward. 😄

Recently I’ve been needing to constantly connect and disconnect from my company’s VPN. As far as I can tell, Windows does not provide an indicator in the system tray regarding the VPN status and clicking on the network icon to see if I was connected was really annoying.

Status

So I decided to code a small utility to make the VPN status more prominent. There might be many ways of detecting if a VPN is active, but this is one of the simplest:

netsh interface show interface

As you can see, my VPN connection is listed, so this means I’m connected to it.

netsh

Next we can just use .Net Process class to spawn the command, get stdout and parse the contents in order to search for our VPN connection. Something similar to this:

private void ScanNetwork(string vpnName) {

    var proc = new Process {
        StartInfo = new ProcessStartInfo {
            FileName = "CMD.EXE",
            Arguments = "/C netsh interface show interface",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    bool found = false;

    proc.Start();
    while (!proc.StandardOutput.EndOfStream) {
        string line = proc.StandardOutput.ReadLine();

        if (line.Contains(vpnName) && line.Contains(" Connected")) {
            found = true;
            break;
        }
    }

    if (found) {
        this.vpnNotifyIcon.Icon = Properties.Resources.Active;
    }
    else {
        this.vpnNotifyIcon.Icon = Properties.Resources.Inactive;
    }

}

Maybe the only tricky part is including the “/C” before netsh.

The rest of the code is just boilerplate code in order to create an icon in the system tray, a menu to quit the application and starting a timer to execute the ScanNetwork method every 3 seconds.

public class VpnStatusApp : ApplicationContext {

    private NotifyIcon vpnNotifyIcon;
    private ContextMenuStrip vpnMenu;
    private ToolStripMenuItem exitMenuItem;
    private Timer myTimer;
    private string vpnName;

    public VpnStatusApp() {
        Init();
        ScanNetwork(vpnName);
        myTimer.Enabled = true;
    }

    private void Init() {
        vpnNotifyIcon = new NotifyIcon();
        vpnMenu = new ContextMenuStrip();
        exitMenuItem = new ToolStripMenuItem();

        vpnMenu.ImageScalingSize = new Size(20, 20);
        vpnMenu.Items.AddRange(new ToolStripItem[] {
        exitMenuItem});
        vpnMenu.Name = "vpnMenu";
        vpnMenu.Size = new Size(103, 28);

        exitMenuItem.Name = "exitToolStripMenuItem";
        exitMenuItem.Size = new Size(210, 24);
        exitMenuItem.Text = "Exit";
        exitMenuItem.Click += new EventHandler(this.ExitMenuItem_Click);

        vpnNotifyIcon.ContextMenuStrip = this.vpnMenu;
        vpnNotifyIcon.Icon = Properties.Resources.Inactive;
        vpnNotifyIcon.Text = "VPN Status";
        vpnNotifyIcon.Visible = true;

        myTimer = new Timer {
            Enabled = false,
            Interval = 3000
        };

        myTimer.Tick += new EventHandler(this.TimerElapsed);

        vpnName = Properties.Settings.Default.VpnName;
    }

    // ScanNetwork goes here

    private void TimerElapsed(object sender, EventArgs e) {
        ScanNetwork(vpnName);
    }

    private void ExitMenuItem_Click(object sender, EventArgs e) {
        Exit();
    }

    private void Exit() {
        vpnNotifyIcon.Visible = false;
        myTimer.Enabled = false;

        Application.Exit();
    }
}

And this is how it looks like when you are disconnected and connected respectively.

Disconnected

Connected

That’s it!. You can find the complete source code here if you are interested. Just in case, once you compile the solution, you can customize your VPN name in vpnStatus.exe.config.

Thanks for reading!!! 😃