Reputation: 135
I have a peculiar issue where my .exe that's build from Visual Studio runs and works perfectly on my computer, but when I test it on others, it doesn't work as intended. The issue in particular is that I have a button that when clicked will disconnect or connect our Always-On VPN connection:
private void _button_ConnectVPN_Click(object sender, EventArgs e)
{
if (Button_ConnectVPN.Text == "Always-On VPN: On")
{
_disableTaskScehdule();
_disconnectAlwaysOnVPN();
}
else if (Button_ConnectVPN.Text == "Always-On VPN: Off")
{
_connectToAlwaysOnVPN();
}
}
The two methods _connectToAlwaysOnVPN() and _disconnectAlwaysOnVPN() works perfectly fine and disconnects or connects the VPN as intended.
I then have this, that continuously update the background color and text of my button depending on the upstate of Always-On:
Timer aTimer = new Timer();
aTimer.Interval = 500;
aTimer.Tick += _updateConnectionStatus;
aTimer.Start();
private void _updateConnectionStatus(object sender, EventArgs e)
{
if(_checkForVPNInterface() == true)
{
Button_ConnectVPN.Text = "Always-On VPN: On";
Button_ConnectVPN.BackColor = System.Drawing.ColorTranslator.FromHtml("#ACD1AF");
}
else if (_checkForVPNInterface() == false)
{
Button_ConnectVPN.BackColor = System.Drawing.ColorTranslator.FromHtml("#F47174");
Button_ConnectVPN.Text = "Always-On VPN: Off";
}
}
As mentioned before, when I run this - either via Visual Studio or the .exe file it creates - it runs perfectly and the background color + text is updated accordingly. On my test machine, the text + background color stays the same. I even tried installing Visual Studio on my test machine and copied the entire solution to it and tried running it through Visual Studio, but it still didn't work as it should.
My initial thought is that I have something that is installed on my computer, which makes it work perfectly - though I don't know what.
Any pointers, comments or anything is much appreciated!
EDIT: Was asked to show the method for _checkForVPNConnection()
private bool _checkForVPNInterface()
{
var lastLine = File.ReadLines(ConfigurationManager.AppSettings["logPath"]).Last();
if (NetworkInterface.GetIsNetworkAvailable())
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterfaces in interfaces)
{
if (networkInterfaces.Description.Contains("Always-On VPN") && networkInterfaces.OperationalStatus == OperationalStatus.Up)
{
if (lastLine == "Always-On VPN - Disconnected")
Logger.WriteLog("Always-On VPN - Connected");
return true;
}
}
}
if (lastLine == "Always-On VPN - Connected")
Logger.WriteLog("Always-On VPN - Disconnected");
return false;
}
Upvotes: 0
Views: 125
Reputation: 135
As @canton7 pointed out in his comment, I ran the debugging on the test machine and found that the Networkinterface both have "Always-On VPN" (which I need) and "Always-On VPN User Tunnel" - which is always up, even if the connection is disconnected.
Because I had:
networkInterfaces.Description.Contains("Always-On VPN") && networkInterfaces.OperationalStatus == OperationalStatus.Up
It worked as it was (poorly) designed ;)
I have now changed the:
networkInterfaces.Description.Contains("Always-On VPN")
to:
networkInterfaces.Description.Equals("Always-On VPN")
And it worked like it should
Upvotes: 1