C# 设置系统代理 - 使用 RegistryKey 类修改代理设置
在 C# 中设置系统代理可以使用 'Microsoft.Win32.Registry' 命名空间中的 'RegistryKey' 类来修改 Windows 注册表中的代理设置。以下是一个示例代码:
using Microsoft.Win32;
public class ProxyHelper
{
public static void SetProxy(string proxyServer, string bypassList)
{
RegistryKey registry = Registry.CurrentUser.OpenSubKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings', true);
if (registry != null)
{
registry.SetValue('ProxyServer', proxyServer);
registry.SetValue('ProxyOverride', bypassList);
registry.SetValue('ProxyEnable', 1);
registry.SetValue('ProxyHttp1.1', 1);
registry.Close();
}
}
public static void DisableProxy()
{
RegistryKey registry = Registry.CurrentUser.OpenSubKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings', true);
if (registry != null)
{
registry.SetValue('ProxyEnable', 0);
registry.Close();
}
}
}
使用示例:
ProxyHelper.SetProxy('proxy.example.com:8080', 'localhost');
// 设置代理服务器为 proxy.example.com:8080,绕过代理的地址列表为 localhost
ProxyHelper.DisableProxy();
// 禁用代理
请注意,修改注册表需要管理员权限,所以在运行此代码时,请以管理员身份运行或者将你的程序打包成一个能够请求管理员权限的可执行文件。
原文地址: https://www.cveoy.top/t/topic/o2Pk 著作权归作者所有。请勿转载和采集!