C# 设置浏览器代理和解除浏览器代理

using System;
using System.Windows.Forms;

// 引入命名空间
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace ProxyFastSetting
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                GetProxy();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        // DllImport:动态端口
        [DllImport(@"wininet",
        SetLastError = true,
        CharSet = CharSet.Auto,
        EntryPoint = "InternetSetOption",
        CallingConvention = CallingConvention.StdCall)]
        // InternetSetOption:互联网设置选项
        public static extern bool InternetSetOption
        (
            int hInternet,
            int dmOption,
            IntPtr lpBuffer,
            int dwBufferLength
         );
        // 设置代理
        public void SetProxy()
        {
            //打开注册表CurrentUser
            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", chkProxyServer.Checked ? 1 : 0);
                string proxyServerAddress = string.Empty, proxyServerPort = string.Empty;
                // 如果为空值,设置默认值
                proxyServerAddress = txtProxyServerAddress.Text == "" ? "192.168.0.1" : txtProxyServerAddress.Text;
                proxyServerPort = txtProxyServerPort.Text == "" ? "80" : txtProxyServerPort.Text;
                optionKey.SetValue("ProxyServer", (proxyServerAddress + ":" + proxyServerPort));
            }
            //激活代理设置    
            InternetSetOption(0, 39, IntPtr.Zero, 0);
            InternetSetOption(0, 37, IntPtr.Zero, 0);
        }
        // 获取代理
        public void GetProxy()
        {
            //打开注册表CurrentUser
            var regKey = Registry.CurrentUser;

            const string subKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
            var optionKey = regKey.OpenSubKey(subKeyPath, true);
            //更改健值,设置代理
            if (optionKey != null)
            {
                // 获取是否使用代理服务器
                string proxyEnable = optionKey.GetValue("ProxyEnable").ToString();
                chkProxyServer.Checked = proxyEnable == "1" ? true : false;

                // 获取地址跟端口
                string addressAndPort = optionKey.GetValue("ProxyServer").ToString();
                txtProxyServerAddress.Text = addressAndPort.Split(':')[0];
                txtProxyServerPort.Text = addressAndPort.Split(':')[1];
            }
        }
        // 保存设置
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SetProxy();// 设置代理服务器
                GetProxy();// 获取代理服务器
                MessageBox.Show("保存成功!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

可打开和关闭代理

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值