|
代码如下:
using System.Runtime.InteropServices;
using Microsoft.Win32;
//You can change the proxy with InternetSetOption method from the wininet.dll, here is a example to set the proxy
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
/// <summary>
/// 设置IE代理
/// </summary>
/// <param name="isNeedProxy">是否需要代理</param>
private static void SettingsIEBrowserProxy(bool isNeedProxy)
{
const string INTERNETSETTINGSKEYPATH = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
RegistryKey settings = null;
try
{
settings = Registry.CurrentUser.OpenSubKey(INTERNETSETTINGSKEYPATH, true);
//更改健值,设置代理,
if (settings != null)
{
if (isNeedProxy)
{
settings.SetValue("ProxyEnable", 1);
//ip.port 就是代理服务器的ip,port就是端口
settings.SetValue("ProxyServer", ip +":" + port);
}
else
{
//取消代理
settings.SetValue("ProxyEnable", 0);
}
}
//激活代理设置
InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (settings != null)
{
settings.Close();
}
}
}
|
|