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.
- Open Visual Studio
- File -> New Project -> Visual C# – Windows -> Windows Forms Application
- Add two textbox controls and two buttons
- 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 } } }
Last Updated on October 26, 2015