C# IE代理操作
1 public class IPProxy 2 { 3 [System.Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)] 4 private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lPBuffer, int lpdwBufferLength); 5 private const int INTERNET_OPTION_REFRESH = 0x000025; 6 private const int INTERNET_OPTION_SETTINGS_CHANGED = 0x000027; 7 8 public static bool Proxy(string ip,int port) { 9 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); 10 //设置代理可用 11 rk.SetValue("ProxyEnable", 1); 12 //设置代理IP和端口 13 rk.SetValue("ProxyServer", string.Format("{0}:{1}", ip, port)); 14 rk.Close(); 15 //使它立即生效 16 InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); 17 InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); 18 if (IsUseful("http://www.baidu.com/")) { 19 return true; 20 } 21 CancelProxy(); 22 return false; 23 } 24 public static void CancelProxy() { 25 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); 26 rk.SetValue("ProxyEnable", 0); 27 } 28 public static bool IsUseful(string ipOrHost) { 29 using (System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping()) 30 { 31 32 try 33 { 34 35 return System.Net.NetworkInformation.IPStatus.Success.Equals(pingSender.Send(ipOrHost,6000).Status); 36 } 37 catch { 38 return false; 39 } 40 } 41 42 } 43 }