[Project Intro] Disable and enable specified web proxy in code
来源:互联网 发布:java图形界面编程工具 编辑:程序博客网 时间:2024/05/17 02:22
Introduction
Explains how to close and open your web proxy using code instead of manually configuring in Internet Options. The latter operation is quite inconvinient thus annoying when you need frequently toggle between on and off states of web proxy.
Solution
Easy! Just configure the web proxy in Windows Registry. Set key 'ProxyEnable' tozero to disable the web proxy and toone to enable it specified by another key named'ProxyServer'.
Using the code (in C#)
First, identify the web proxy with application settings wrapper class ProxySettings.ProxySettings inherits fromApplicationSettingsBase class which uses reflection to detect these attributes at run time. Most of this information gets passed to the settings provider layer, which is responsible for storage, persistence format, and so on. All attributes have a default setting value applied using DefaultSettingValue and can be modified later at run time.
/// <summary>/// Web proxy identifier/// </summary>sealed class ProxySettings : ApplicationSettingsBase{ [UserScopedSetting] [DefaultSettingValue("10.27.7.110")] public string Server { get { return (string)this["Server"]; } set { this["Server"] = value; } } [UserScopedSetting] [DefaultSettingValue("8080")] public string Port { get { return (string)this["Port"]; } set { this["Port"] = value; } }}
Second, enable and disable the web proxy by configuring the Internet Settings in Registry.
/// <summary>/// Toggle between on and off states of specified web proxy/// </summary>/// <param name="proxy">Specify the proxy identity. Should be formated as '<ip>:<port>'</param>public void ToggleProxy(string proxy){ zhuceKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); if (proxy == null || proxy.Length == 0) //disable web proxy { regKey.SetValue("ProxyEnable", 0); } else // enable specified web proxy { regKey.SetValue("ProxyEnable", 1); regKey.SetValue("ProxyServer", proxy); } regKey.Flush(); regKey.Close(); InternetSetOption(0, 39, IntPtr.Zero, 0); InternetSetOption(0, 37, IntPtr.Zero, 0);}
Downloads
I have implemented a small Windows-Forms-based tool to easily switch between the two states of web proxy. You can modify the default proxy accordingly in ProxySettings class. Once the enable/disable button is clicked, this application goes to an icon in the notification area at the right bottom of your computer screen (via NotifyIcon).
Figure. The simple UI (left: proxy off ; right: proxy on)
Download Microsoft Windows Form-based web proxy on-off tool (source codes) -22k
- [Project Intro] Disable and enable specified web proxy in code
- enable/disable ie proxy
- Docker: How to enable/disable HTTP Proxy in Toolbox
- How to Enable and Disable Root Login in Ubuntu
- Disable or Enable SSH Root Login and Limit SSH Access in Linux
- How to Enable or Disable DEP in Vista, Windows 7, and Windows 8
- To enable and disable Portal Trace
- Disable and enable a control using javascript
- Enable and disable ArcGIS Server Security
- CMake Intro - How to enable VS project Unicode
- Disable and enable Foreign Key and Check Constraints
- Disable offline mode and sync project错误
- Disable Gradle 'offline mode' and sync project
- how to enable or disable registry editing in xp
- how to enable /disable networking by command in ubuntu12.04
- Mac OS X: Enable/Disable/Login Root user and Others
- Mysql - disable TCP/IP connections and enable named pipes
- 如何扩展主键的长度(DISABLE And ENABLE CHANGE_TRACKING)
- iOS常用的高效开发宏
- 如何修改用户SSH\telnet登陆linux时候显示的欢迎信息
- 单播、广播和多播IP地址
- 朴素贝叶斯(NaiveBayes)算法总结
- CUnit-2.1-3.tar.bz2 安装以及遇到问题的解决办法
- [Project Intro] Disable and enable specified web proxy in code
- 织梦CMS XML地图插件(支持Google地图、Baidu地图、RSS地图)
- Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法
- Android4.0 WIFI文档翻译
- 程序员的浪漫
- 数组给数组赋值问题
- Modbus协议简单总结
- 压缩追踪Compressive Tracking源码理解
- 搭建自己的ffmpeg的android移植开发环境