C# 检测默认web浏览器的代理设置

C# 检测默认web浏览器的代理设置,c#,proxy,C#,Proxy,MSDN样品 HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); WebProxy myProxy=new WebProxy(); // Obtain the 'Proxy' of the Default browser. myProxy=(WebProxy)myWebRequest.Proxy; 不起作用。 我得到的错误是:无法将“WebProxyWrapp

MSDN样品

HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the  Default browser.  
myProxy=(WebProxy)myWebRequest.Proxy;
不起作用。 我得到的错误是:无法将“WebProxyWrapper”类型的对象强制转换为“System.Net.WebProxy”类型

我有哪些选项?

返回一个
界面,而不是
WebProxy
。改变这一点,它就会起作用


您也可以使用或来获取代理详细信息,而不必进行
HttpWebRequest
并从中获取代理。

要检查自动检测设置,请使用代码:

RegistryKey registry = Registry.CurrentUser.OpenSubKey(
    "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 0);
RegistryKey registry2 = Registry.CurrentUser.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", true);
registry2.DeleteValue("DefaultConnectionSettings", false);
registry2.DeleteValue("SavedLegacySettings", false);

这段代码当然不会检查设置,但会更改和删除它们。