要实时更新IE所有实例需调用如下API:
[DllImport(
"
wininet.dll
"
, SetLastError
=
true
)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
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 );
}
{
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;
}
}
{
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;
}
}