IE5/IE6/IE7/IE8的代理服务器的设置与取消(C#.Net)实现

来源:互联网 发布:供应链网络节点 编辑:程序博客网 时间:2024/06/07 01:50

要实时更新IE所有实例需调用如下API:

[DllImport("wininet.dll", SetLastError = true)]
        
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

 

然后刷新IE设置:

private static void RefreshIESettings()
        {
            
const int INTERNET_OPTION_REFRESH = 0x000025;
            
const int INTERNET_OPTION_SETTINGS_CHANGED = 0x000027;
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 
0);
            InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 
0);
        }

 

最后写IE在注册表中的设置即可:

 

public static void SetIEProxy(Proxy proxy)
        
{
            
string proxyIP = "";
            proxyIP
=string.Format("{0}:{1}", proxy.ProxyAddr, proxy.ProxyPort);
            RegistryKey rk 
= Registry.CurrentUser.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Internet Settings"true);
            rk.SetValue(
"ProxyEnable"1);
            rk.SetValue(
"ProxyServer", proxyIP);
            rk.Close();
            RefreshIESettings();
        }


        
public static void CancelIEProxy()
        
{
            RegistryKey rk 
= Registry.CurrentUser.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Internet Settings",true);
            rk.SetValue(
"ProxyEnable"0);
            rk.Close();
            RefreshIESettings();
        }


public class Proxy
    
{
        
private string proxyName;
        
private string proxyAddr;
        
private int port;

        
public Proxy(string name, string addr, int port)
        
{
            
this.proxyName = name;
            
this.proxyAddr = addr;
            
this.port = port;
        }


        
public string ProxyName get return proxyName; } }
        
public string ProxyAddr get return proxyAddr; } }
        
public int ProxyPort get return port; } }

        
public override string ToString()
        
{
            
return proxyName;
        }

    }