有人还发现:在window7下, 在一个进程中, 设置和取消不能都执行,---- 要么设置,要么取消。 但如果第一次运行时,只进行设置代理,退出后再进运行,只进行取消,这是没有问题的。 

简单说说中医中药网(www.yiyaojing.com)给出的解决方案:每次设置或取消代理时,都新建一个进程,在新的进程中处理,处理完之后关掉进程。

这种方法可以work,但显得很蛋疼。

所以我没采用这种方法。

最后在同城交友网(www.niyuewo.com)上找到了一个国外一大神 Joel 'Jaykul' Bennett 的一篇文章Setting Windows internet connection proxy from C#

试了一下,完美解决。

代码如下:

using System;
using System.Runtime.InteropServices;
using System.ComponentModel; namespace PoshHttp
{
public class Proxies
{
public static bool UnsetProxy()
{
return SetProxy(null, null);
}
public static bool SetProxy(string strProxy)
{
return SetProxy(strProxy, null);
} public static bool SetProxy(string strProxy, string exceptions)
{
InternetPerConnOptionList list = new InternetPerConnOptionList(); int optionCount = string.IsNullOrEmpty(strProxy) ? : (string.IsNullOrEmpty(exceptions) ? : );
InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
// USE a proxy server ...
options[].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
options[].m_Value.m_Int = (int)((optionCount < ) ? PerConnFlags.PROXY_TYPE_DIRECT : (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
// use THIS proxy server
if (optionCount > )
{
options[].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER;
options[].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
// except for these addresses ...
if (optionCount > )
{
options[].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
options[].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
}
} // default stuff
list.dwSize = Marshal.SizeOf(list);
list.szConnection = IntPtr.Zero;
list.dwOptionCount = options.Length;
list.dwOptionError = ; int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
// make a pointer out of all that ...
IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
// copy the array over into that spot in memory ...
for (int i = ; i < options.Length; ++i)
{
IntPtr opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize));
Marshal.StructureToPtr(options[i], opt, false);
} list.options = optionsPtr; // and then make a pointer out of the whole list
IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize);
Marshal.StructureToPtr(list, ipcoListPtr, false); // and finally, call the API method!
int returnvalue = NativeMethods.InternetSetOption(IntPtr.Zero,
InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION,
ipcoListPtr, list.dwSize) ? - : ;
if (returnvalue == )
{ // get the error codes, they might be helpful
returnvalue = Marshal.GetLastWin32Error();
}
// FREE the data ASAP
Marshal.FreeCoTaskMem(optionsPtr);
Marshal.FreeCoTaskMem(ipcoListPtr);
if (returnvalue > )
{ // throw the error codes, they might be helpful
throw new Win32Exception(Marshal.GetLastWin32Error());
} return (returnvalue < );
}
} #region WinInet structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InternetPerConnOptionList
{
public int dwSize; // size of the INTERNET_PER_CONN_OPTION_LIST struct
public IntPtr szConnection; // connection name to set/query options
public int dwOptionCount; // number of options to set/query
public int dwOptionError; // on error, which option failed
//[MarshalAs(UnmanagedType.)]
public IntPtr options;
}; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InternetConnectionOption
{
static readonly int Size;
public PerConnOption m_Option;
public InternetConnectionOptionValue m_Value;
static InternetConnectionOption()
{
InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption));
} // Nested Types
[StructLayout(LayoutKind.Explicit)]
public struct InternetConnectionOptionValue
{
// Fields
[FieldOffset()]
public System.Runtime.InteropServices.ComTypes.FILETIME m_FileTime;
[FieldOffset()]
public int m_Int;
[FieldOffset()]
public IntPtr m_StringPtr;
}
}
#endregion #region WinInet enums
//
// options manifests for Internet{Query|Set}Option
//
public enum InternetOption : uint
{
INTERNET_OPTION_PER_CONNECTION_OPTION =
} //
// Options used in INTERNET_PER_CONN_OPTON struct
//
public enum PerConnOption
{
INTERNET_PER_CONN_FLAGS = , // Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags
INTERNET_PER_CONN_PROXY_SERVER = , // Sets or retrieves a string containing the proxy servers.
INTERNET_PER_CONN_PROXY_BYPASS = , // Sets or retrieves a string containing the URLs that do not use the proxy server.
INTERNET_PER_CONN_AUTOCONFIG_URL = //, // Sets or retrieves a string containing the URL to the automatic configuration script. } //
// PER_CONN_FLAGS
//
[Flags]
public enum PerConnFlags
{
PROXY_TYPE_DIRECT = 0x00000001, // direct to net
PROXY_TYPE_PROXY = 0x00000002, // via named proxy
PROXY_TYPE_AUTO_PROXY_URL = 0x00000004, // autoproxy URL
PROXY_TYPE_AUTO_DETECT = 0x00000008 // use autoproxy detection
}
#endregion internal static class NativeMethods
{
[DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
}
}

c#如何在win7下设置IE代理的完美解决方案的更多相关文章

  1. 详细介绍如何在win7下首次实现通过Git bash向Github提交项目

    详细介绍如何在win7下首次实现通过Git bash向Github提交项目 引自:http://jingpin.jikexueyuan.com/article/35944.html 作者: wddoe ...

  2. [计算机]如何在win7下查看并更改文件的默认后缀名

    如何在win7下查看默认文件的后缀名并更改呢? 例如有一个文件本来是exe,想变更为txt.但是无法看到后缀名,就无法更改. 双击桌面上的计算机图标,或者任意盘符界面,单击如下图左侧“组织”右侧的下拉 ...

  3. win7下设置挂载Linux服务器nfs共享的数据 -- 转

    最近学习NFS文件系统的使用,Ubuntu上配置好了,想和Win7共享数据,所以网上搜到了这篇文章.借花献佛,跟大家共享一下: http://www.2cto.com/os/201207/139132 ...

  4. navicat for mysql 在win7下设置定时计划之导出数据处理

    navicat for mysql 在win7下设置定时计划之导出数据处理 博客分类: mysql navitcatmysql定时任务导出  前两篇记录了,navicat for mysql计划的入门 ...

  5. Win7下设置WiFi热点

    Win7下设置WiFi热点 今天研究了下Win7设置WIFI热点,Connectify神马的都是浮云~亲測可用,现拿出来分享下~ 1.点击"開始",再点击"执行" ...

  6. 如何在WIN7下进行LINUX虚拟机搭建

    Linux是一套免费使用和自由传播的类Unix操作系统,非常适用于搭建网络服务器等,我本人日常工作时,是使用的LINUX和WIN7双操作系统,但每次更换系统总要关机重启很不方便,所以也在WIN7下搭建 ...

  7. win7下设置环境变量

    手工当然可以进行环境变量的设置,但是如果一个小组有需要设置固定环境变量的操作,这可能就会有点麻烦了,xp下设置环境变量比较简单,直接用set,win7下需要使用setx SETX XX_HOME &q ...

  8. linux下设置git代理访问.

    有时候克隆仓库巨慢无比,需要设置代理. 一般情况下 proxychains 可以搞定的. 但是某些情况,如go 安装模块的时候是调用git的.这个时候proxchains就不行了. go 也可以通过设 ...

  9. 【备忘】Windows的命令行下设置网络代理

    在公司访问网络的时候,需要设置代理,设置浏览器的代理是相当的方便了.但有的时候要使用命令行,就需要自己设置了(貌似只要浏览器设置好了,可以直接使用命令行,但我们公司的借,需要有用户名和密码,如果没有使 ...

随机推荐

  1. 25个完美的Opencart模板,让顾客无法抗拒!

    在线开展业务是当前的流行趋势.OpenCart(点击这里下载),作为一个流行的开源PHP电子商务系统,获取OpenCart模板不在像以前那么困难了.OpenCart容易安装,模板支持良好,扩展功能包十 ...

  2. DNN 错误代码 0x80070005 解决方案

    在IIS上创建DNN站点,可能出现的错误代码:0x80070005,因为权限不足而不能访问DNN. 解决方法:打开IIS, 1.右键目标网站->编辑权限->安全->添加组或者用户 “ ...

  3. WPF 简介

    简介 一.   WPF产生的背景 因为人们的生活水平不断提前,审美观也随着提升,而软件的应用发展水平目前无法赶上大家的审美观和使用要求:比如:像电影中的软件能够方便的使用,而且有动态的效果同时附加形象 ...

  4. [cocos2d-js]chipmunk例子(一)

    initChipmunk:function() { this.space = new cp.Space(); this.setupDebugNode(); //设置空间内刚体间联系的迭代计算器个数和弹 ...

  5. dom 动态生产表格

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. LeetCode100:Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  7. emWin(ucGui)数值显示例程 -【worldsing笔记】

    本例程下载:2.emWin5.26(ucGui)VS2008数字显示.zip   在emWin显示文本字符还是容易,我们也可以使用字符串和标准 C 库的函数来显示数值.然而,有时候这会是件困难的事.通 ...

  8. 结构类模式(四):装饰(Decorator)

    定义 动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 特点 装饰对象和真实对象有相同的接口.这样客户端对象就能以 ...

  9. sql waitfor 延时执行

    看MSDN:http://msdn.microsoft.com/zh-cn/library/ms187331.aspx 语法为: WAITFOR { DELAY 'time_to_pass' | TI ...

  10. Fragment进阶

    fragment之间的通信,fragment和Activity生命周期之间的关系 通过上一篇浅显的学习了一下,怎么在Activity中添加fragment.在介绍fragment之间的通信之前,我们来 ...