Do not restart IE to modify proxy settings

Source: Internet
Author: User

I believe everyone is familiar with setting proxy servers through Internet Options. However, in some cases, a bunch of proxy IP addresses are required for switching, which makes it inconvenient. Then we can implement the next switch through the gadgets.
First, we will introduce the two keys in the registry.
HKEY_CURRENT_USER/software/Microsoft/Windows/CurrentVersion/Internet Settings
Proxyserver: sets the proxy server address and port number.
Proxyenable: Set whether to enable Proxy Server

Knowing the principles, the code is easy to write.

1 registrykey key = registry. currentuser. opensubkey ("software // Microsoft // windows // CurrentVersion // Internet Settings", true );
2 key. setvalue ("proxyserver", this. proxylibs [Index]);
3 key. setvalue ("proxyenable", 1 );
4 key. Close ();

Although this implementation is simple, there is still a problem. After the registry is set, the current IE window cannot take effect immediately. The configuration takes effect only when a new window is opened. in many cases, the window cookie is disabled, and the meaning of modifying the proxy IP address is lost.

The following describes a function in wininet and sets an Internet option to make the configured configuration take effect immediately. Check the code.

 

1 [dllimport ("wininet. dll", setlasterror = true)]
2 Private Static extern bool internetsetoption (intptr hinternet, int dwoption, intptr lpbuffer, int lpdwbufferlength );
3 private const int internet_option_refresh = 0x000025;
4 private const int internet_option_settings_changed = 0x000027;
5 internetsetoption (intptr. Zero, internet_option_settings_changed, intptr. Zero, 0 );
6 internetsetoption (intptr. Zero, internet_option_refresh, intptr. Zero, 0 );
7

Complete code

 

1 // ============================================== ========================
2 // 100 M dual-line space + 50 m database + M mailbox 28 RMB/year
3 // 200 M dual-line space + 50 m database + M mailbox 38 RMB/year
4 // 300 M dual-line space + 50 m database + M mailbox 48 RMB/year
5 // 600 m dual-line space + M database + M mailbox 80 RMB/year
6 // 1g dual-line space + 100 m database + M mailbox RMB/year
7
8 // special host. If you need m, QQ: 70975363
9
10 // =================================================== ========================
11 public class proxymanager
12 {
13 [dllimport ("wininet. dll", setlasterror = true)]
14 Private Static extern bool internetsetoption (intptr hinternet, int dwoption, intptr lpbuffer, int lpdwbufferlength );
15
16 private const int internet_option_refresh = 0x000025;
17 private const int internet_option_settings_changed = 0x000027;
18
19 private list <string> proxylibs = new list <string> ();
20
21 private void reflush ()
22 {
23 internetsetoption (intptr. Zero, internet_option_settings_changed, intptr. Zero, 0 );
24 internetsetoption (intptr. Zero, internet_option_refresh, intptr. Zero, 0 );
25}
26
27
28 public proxymanager ()
29 {
30 openfiledialog OPF = new openfiledialog ();
31 if (OPF. showdialog (). Equals (dialogresult. OK ))
32 {
33 string [] proxylist = file. readalllines (OPF. filename, encoding. Default );
34 For (INT I = 0; I <proxylist. length; I ++)
35 This. proxylibs. Add (proxylist [I]);
36}
37}
38
39 public void setproxy (INT index)
40 {
41 registrykey key = registry. currentuser. opensubkey ("software // Microsoft // windows // CurrentVersion // Internet Settings", true );
42 key. setvalue ("proxyserver", this. proxylibs [Index]);
43 key. setvalue ("proxyenable", 1 );
44 key. Close ();
45 this. reflush ();
46}
47}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.