通过对注册表操作实现对浏览器的代理设置和取消,操作极其方便。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ProxyFastSetting
{
public partial class FrmMain : Form
{
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "InternetSetOption",
CallingConvention = CallingConvention.StdCall)]
public static extern bool InternetSetOption
(
int hInternet,
int dmOption,
IntPtr lpBuffer,
int dwBufferLength
);
public FrmMain()
{
InitializeComponent();
}
private void Button1Click(object sender, EventArgs e)
{
SetProxy(true);
Close();
}
private void Button2Click(object sender, EventArgs e)
{
SetProxy(false);
Close();
}
public void SetProxy(bool bOpen)
{
//打开注册表
var regKey = Registry.CurrentUser;
const string subKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
var optionKey = regKey.OpenSubKey(subKeyPath, true);
//更改健值,设置代理,
if (optionKey != null) optionKey.SetValue("ProxyEnable", bOpen?1:0);
//激活代理设置
InternetSetOption(0, 39, IntPtr.Zero, 0);
InternetSetOption(0, 37, IntPtr.Zero, 0);
}
}
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ProxyFastSetting
{
public partial class FrmMain : Form
{
[DllImport(@"wininet",
SetLastError = true,
CharSet = CharSet.Auto,
EntryPoint = "InternetSetOption",
CallingConvention = CallingConvention.StdCall)]
public static extern bool InternetSetOption
(
int hInternet,
int dmOption,
IntPtr lpBuffer,
int dwBufferLength
);
public FrmMain()
{
InitializeComponent();
}
private void Button1Click(object sender, EventArgs e)
{
SetProxy(true);
Close();
}
private void Button2Click(object sender, EventArgs e)
{
SetProxy(false);
Close();
}
public void SetProxy(bool bOpen)
{
//打开注册表
var regKey = Registry.CurrentUser;
const string subKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
var optionKey = regKey.OpenSubKey(subKeyPath, true);
//更改健值,设置代理,
if (optionKey != null) optionKey.SetValue("ProxyEnable", bOpen?1:0);
//激活代理设置
InternetSetOption(0, 39, IntPtr.Zero, 0);
InternetSetOption(0, 37, IntPtr.Zero, 0);
}
}