Click here to Skip to main content
15,890,882 members
Articles / Programming Languages / C# 4.0
Tip/Trick

C# Programming - Enable Disable Proxy IP

Rate me:
Please Sign up or sign in to vote.
3.75/5 (6 votes)
3 Jan 2018CPOL1 min read 22.1K   9   2
This is a simple program which demonstrates how we can change the proxy settings programmatically.

Introduction

When we are connecting VPN, multiple times, we have to enable disable proxy and change the IP address. For those who want this to happen programmatically, the below code snippet will help.

Background

As a normal process, we use the standard steps given below to change the proxy settings.

Internet Explorer --> Settings --> Internet options --> Connections --> LAN settings

Using the Code

I am bored of opening the internet options every time to change the settings. I started to write code for that and while doing so, I got the interesting information below.

  1. Registry: Registry class helps us to read/write internet proxy settings from code. Using this registry, we can perform modifications on LAN settings.
  2. InternetSetOption

Once we modify the changes, it will not reflect immediately. As process, we have to restart our machine, else we have to use this method to run in the code. It will refresh the settings without restarting the machine.

C#
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ProxyOnOff
{
    class Program
    {
        [DllImport("wininet.dll")]
        public static extern bool InternetSetOption
          (IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
        public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
        public const int INTERNET_OPTION_REFRESH = 37;
        
        static void Main(string[] args)
        {
                int a; 
                bool settingsReturn, refreshReturn;

                Console.WriteLine("*************Proxy ON OFF*************\n");
                Console.WriteLine("1 for ON\n");
                Console.WriteLine("0 for OFF\n");
                
                a= Convert.ToInt32(Console.ReadLine());

            RegistryKey registry = Registry.CurrentUser.OpenSubKey
               ("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
           
            switch (a)
                {
                    case 1:
                        {
                            registry.SetValue("ProxyEnable", 1);
                            registry.SetValue
                            ("ProxyServer", "Your proxy IP:8080");
                            if ((int)registry.GetValue("ProxyEnable", 0) == 0)
                                Console.WriteLine("Unable to enable the proxy.");
                            else
                                Console.WriteLine("The proxy has been turned on.");
                            break;
                        }
                    case 0:
                        {
                            registry.SetValue("ProxyEnable", 0);
                            registry.SetValue("ProxyServer", 0);
                            if ((int)registry.GetValue("ProxyEnable", 1) == 1)
                                Console.WriteLine("Unable to disable the proxy.");
                            else
                                Console.WriteLine("The proxy has been turned off.");
                         
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("Invalid Argument!");
                            Console.ReadKey();
                            return;
                        }
                }
            registry.Close();
            settingsReturn = InternetSetOption
            (IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            refreshReturn = InternetSetOption
            (IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
            Console.WriteLine("Press any key to exit...");
        }
    }
}

To create a program, click on Visual Studio --> New --> Console Application.
In program.cs, paste the above code and publish it.

Points of Interest

This is my first tip on Code Project. I am happy to post it.

History

  • 3rd January, 2018: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Galene Leah29-Jan-23 0:15
Galene Leah29-Jan-23 0:15 
BugOn window 7 it enables or disables only once Pin
vipin580512-Feb-19 6:55
vipin580512-Feb-19 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.