Search code examples
c#internet-explorerproxybinaryregistry

C# Registry "Use automatic configuration script", Changing Binary values


I am writing a script that will change the proxy settings in Internet Explorer for some of our VPN users that are on and off the network frequently. The location of the registry value is located HKEY_CURRENT_USER - Software - Microsoft - Windows - Current Version - Internet Settings - Connections - DefaultConnectionSettings. The value that I am trying to change is in IE, under LAN Settings, I want to uncheck the "Use automatic configuration script" check box. Ideally what I would like to do is ONLY change the 9th key to "09" and leave the rest of the Binary value alone. I have never had to write in binary before so I am not really sure where to start but below is my code

    static void Main()
    {
        string strRegPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
        string strAutoConfig = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections";

        //First need to close all web browsers
        try
        {

            foreach (Process proc in Process.GetProcessesByName("Firefox"))
            {
                proc.Kill();
                Console.WriteLine("{0} {1}", "Killed the", proc);
            }
            foreach (Process proc in Process.GetProcessesByName("iexplore"))
            {
                proc.Kill();
                Console.WriteLine("{0} {1}", "Killed the", proc);
            }
            foreach (Process proc in Process.GetProcessesByName("chrome"))
            {
                proc.Kill();
                Console.WriteLine("{0} {1}", "Killed the", proc);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

        if ((int)Registry.GetValue(strRegPath, "ProxyEnable", 0) == 0)
        {
            Console.WriteLine();
            Console.WriteLine("The proxy was already set to Automatically Detect Settings");
            Console.WriteLine();
            Console.WriteLine("Press Enter to Exit");
        }
        else
        {
            Registry.SetValue(strRegPath, "ProxyEnable", 0);
            Console.WriteLine();
            Console.WriteLine("Proxy settings have been changed to Automatically Detect Settings");
            Console.WriteLine();
            Console.WriteLine("Press Enter to Exit");
        }

        Console.ReadKey();
        return;

    }
}

Solution

  • If you use the below then it will force Internet Explorer to update the default settings back to only check the Automatically Detect Settings box and will leave the others blank

    Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", "DefaultConnectionSettings", new byte[] { 00 });