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);
            }
        }
    }
}

可打开和关闭代理

猜你喜欢

转载自blog.csdn.net/feelsyt/article/details/128401009
今日推荐