C#高级:通过修改注册表实现系统代理服务器的启用、编辑和禁用

发布于:2025-07-30 ⋅ 阅读:(30) ⋅ 点赞:(0)

一、系统代理服务器-简介

        系统代理服务是中介服务器,负责转发和处理网络请求,隐藏用户真实IP,提升安全性与隐私保护,优化网络性能并控制访问。常用于缓存数据、过滤内容和绕过地理限制。

二、意义

  1. 保护隐私和匿名性:通过隐藏用户的真实IP地址,代理服务器保护了用户的在线隐私。
  2. 提升网络性能:缓存技术让代理服务器能够减少重复请求,提高响应速度和带宽使用效率。
  3. 访问控制:企业和组织可以通过代理服务器对员工的网络使用进行管理,限制某些不必要或危险的访问。
  4. 绕过限制和防火墙:代理服务器能帮助用户绕过地理位置限制,访问特定区域不可用的内容。 

三、查询

左下角搜索【代理】进去即可查询

四、代码

public class Program
{
    public static void GetSystemProxySettings()
    {
        using (RegistryKey registry = Registry.CurrentUser.OpenSubKey(
        "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"))
        {
            if (registry != null)
            {
                int proxyEnabled = (int)registry.GetValue("ProxyEnable", 0);
                string proxyServer = registry.GetValue("ProxyServer", "").ToString();

                Console.WriteLine($"代理启用状态: {proxyEnabled}");
                Console.WriteLine($"代理服务器: {proxyServer}");
            }
        }
    }

    public static void SetSystemProxy(string proxyAddress)
    {
        string registryKey = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";

        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryKey, true))
        {
            if (key != null)
            {
                // 启用代理
                key.SetValue("ProxyEnable", 1);

                // 设置代理服务器地址
                key.SetValue("ProxyServer", proxyAddress);

                // (可选)设置绕过代理的地址
                key.SetValue("ProxyOverride", "<-loopback>");
            }
            Console.WriteLine($"代理启用成功:{proxyAddress}");
            //RefreshIESettings();
        }
    }    
    
    public static void DisableSystemProxy()
    {
        string registryKey = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";

        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryKey, true))
        {
            if (key != null)
            {
                // 启用代理
                key.SetValue("ProxyEnable", 0);

                // 设置代理服务器地址
                // 如果存在,删除 ProxyServer
                if (key.GetValue("ProxyServer") != null)
                {
                    key.DeleteValue("ProxyServer");
                    Console.WriteLine("已删除代理服务器设置");
                }

                // (可选)设置绕过代理的地址
                key.SetValue("ProxyOverride", "127.0.0.1;<local>");

                Console.WriteLine($"已删除代理!");
            }
        }
        
    }

    static int Main(string[] args)
    {
        GetSystemProxySettings();
        SetSystemProxy("http=127.0.0.1:8888;https=127.0.0.1:8888");
        GetSystemProxySettings();
        DisableSystemProxy();
        return 0;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到