C#Registry“使用自动配置脚本”,更改二进制值

时间:2016-03-28 14:41:45

标签: c# internet-explorer proxy binary registry

我正在编写一个脚本,该脚本将更改Internet Explorer中代理设置,以便我们的某些VPN用户经常在网络上和网络外。注册表值的位置位于HKEY_CURRENT_USER - 软件 - Microsoft - Windows - 当前版本 - Internet设置 - 连接 - DefaultConnectionSettings。我想要更改的值是在IE中,在“局域网设置”下,我想取消选中“使用自动配置脚本”复选框。理想情况下,我想要做的只是将第9个键更改为“09”并保留剩余的二进制值。我以前从来没有写过二进制文件所以我不确定从哪里开始,但下面是我的代码

    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;

    }
}

2 个答案:

答案 0 :(得分:0)

如果您使用以下内容,则会强制Internet Explorer将默认设置更新为仅选中“自动检测设置”框,并将其他设置保留为空白

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

答案 1 :(得分:0)

我觉得这更好地回答了OP提出的问题:

string strPath = 
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections";

//get the registry value and cast it to a byte array
var RegValue = Registry.GetValue(strPath, "DefaultConnectionSettings", "Failed") as byte[];  

//update the registry value
RegValue[8] = 5; 

//set the new value to the registry
Registry.SetValue(strPath, "DefaultConnectionSettings", RegValue);