Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Proxy Switch

Posted on June 18, 2015October 26, 2015 By David Kittell

Recently had a need to be able to switch on/off proxy at will and got tired of going to Internet Explorer options to set/change it. Hopefully someone else will get benefit from this nice tool.

  1. Open Visual Studio
    1. File -> New Project -> Visual C# – Windows -> Windows Forms Application
  2. Add two textbox controls and two buttons
  3. Double Click on each button to create the OnClick event
#region Using Statements - Start
#region Default Using Statements - Start
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
#endregion Default Using Statements - Stop

#region Special Using Statements - Start
using System.Security.Permissions;
using Microsoft.Win32;
using System.Diagnostics;
#endregion Special Using Statements - Stop
#endregion Using Statements - Stop

namespace ProxySwitch
{
	public partial class Form1 : Form
	{
		public RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
		public int nProxyStatus = 0;

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			nProxyCheckStatus();
		}

		private void btnSetProxy_Click(object sender, EventArgs e)
		{
			btnSetProxy.Enabled = false;
			btnProxySwitch.Enabled = false;


			if (!bEmptyNullCheck(tbProxyIP.Text) && !bEmptyNullCheck(tbProxyPort.Text))
			{
				CheckBrowsers();

				registry.SetValue("ProxyServer", tbProxyIP.Text + ":" + tbProxyPort.Text);

				registry.SetValue("ProxyEnable", 1);

				nProxyCheckStatus();

				btnSetProxy.Enabled = true;
				btnProxySwitch.Enabled = true;
			}
			else
			{
				MessageBox.Show("In order to set/change the proxy both textboxes need information", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
				btnSetProxy.Enabled = true;
				btnProxySwitch.Enabled = true;
			}
		}

		private void btnProxySwitch_Click(object sender, EventArgs e)
		{
			btnSetProxy.Enabled = false;
			btnProxySwitch.Enabled = false;

			CheckBrowsers();

			switch (nProxyStatus)
			{
				case 0:
					//Enable Proxy
					//registry.SetValue("ProxyServer", 1);
					registry.SetValue("ProxyEnable", 1);
					btnProxySwitch.Text = "Disable Proxy";
					break;
				case 1:
					//Disable Proxy
					//registry.SetValue("ProxyServer", 0);
					registry.SetValue("ProxyEnable", 0);
					btnProxySwitch.Text = "Enable Proxy";
					break;
			}

			btnSetProxy.Enabled = true;
			btnProxySwitch.Enabled = true;
		}

		private void nProxyCheckStatus()
		{
			nProxyStatus = (int)registry.GetValue("ProxyEnable");

			string sProxyServer = (string)registry.GetValue("ProxyServer");
			string[] saProxyServer = sProxyServer.Split(':');

			tbProxyIP.Text = saProxyServer[0];
			tbProxyPort.Text = saProxyServer[1];


			switch (nProxyStatus)
			{
				case 0:
					btnProxySwitch.Text = "Enable Proxy";
					break;
				case 1:
					btnProxySwitch.Text = "Disable Proxy";
					break;
			}
		}

		private bool bEmptyNullCheck(string sValue)
		{
			bool bEmpty = true;

			if (!string.IsNullOrEmpty(sValue) && !string.IsNullOrWhiteSpace(sValue))
			{
				bEmpty = false;
			}

			return bEmpty;
		}

		private void CheckBrowsers()
		{
			#region Check If Browsers Are Open - Start
			Process[] pIE = Process.GetProcessesByName("IEXPLORE");
			Process[] pChrome = Process.GetProcessesByName("chrome");

			if (pIE.Length != 0 || pChrome.Length != 0)
			{
				DialogResult dialogResult = MessageBox.Show("In order for this to work Chome and Internet Explorer need to be closed." + Environment.NewLine + Environment.NewLine + "Would you like to continue? If Yes we will forcefully close Chrome and Internet Explorer", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
				if (dialogResult == DialogResult.Yes)
				{
					foreach (System.Diagnostics.Process proc in pIE)
					{
						try
						{
							proc.Kill(); // Close it down.
						}
						catch (Exception ex)
						{
							MessageBox.Show("Unable to close Internet Explorer" + Environment.NewLine + Environment.NewLine + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
					// Wait 10 seconds.
					System.Threading.Thread.Sleep(10000);
					foreach (System.Diagnostics.Process proc in pChrome)
					{
						try
						{
							proc.Kill(); // Close it down.
						}
						catch (Exception ex)
						{
							MessageBox.Show("Unable to close Chrome" + Environment.NewLine + Environment.NewLine + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
					// Wait 10 seconds.
					System.Threading.Thread.Sleep(10000);
				}
				else if (dialogResult == DialogResult.No)
				{
					MessageBox.Show("Closing Proxy Switch for now, please try again when you do not have your browser windows open.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
					Application.Exit();
				}
			}
			#endregion Check If Browsers Are Open - Stop
		}
	}
}
Originally Posted on June 18, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code CSharp

Post navigation

Previous post
Next post

Related Posts

Validate Links

Posted on August 15, 2013October 26, 2015

function validatelink($link) { // Create a curl handle to a location $ch = curl_init($link); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_NOBODY, true); if (curl_exec($ch) === false) { echo ‘INVALID: ‘ . $link . " " . curl_error($ch); } else { echo ‘VALID: ‘ . $link; } // Close handle curl_close($ch); } Example

Read More

Mac OS X – Get Network Information

Posted on February 27, 2017May 2, 2017

Every once in a while you need to get some basic network information from your Mac or a Mac you are supporting, this script below will help get you some helpful information. #!/bin/sh clear sExternalMACALService="http://dns.kittell.net/macaltext.php?address=" # List all Network ports NetworkPorts=$(ifconfig -uv | grep ‘^[a-z0-9]’ | awk -F : ‘{print…

Read More

Raspberry Pi – Dynamic MOTD

Posted on September 12, 2019September 22, 2020

Similar to UNIX – Set SSH Banner but this will update at every login incase something changes. mkdir /etc/update-motd.d cd /etc/update-motd.d touch 00-header && touch 11-server-info && touch 99-footer chmod +x /etc/update-motd.d/* rm /etc/motd ln -s /var/run/motd /etc/motd #!/bin/bash # Functions – Start # Function to convert IP Subnet Mask…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes