编程设置浏览器代理设置在C# [英] Programmatically Set Browser Proxy Settings in C#

查看:160
本文介绍了编程设置浏览器代理设置在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个WinForms应用程序,需要设置Internet Explorer的代理服务器设置,然后打开一个新的浏览器窗口。

I'm writing an winforms app that needs to set internet explorer's proxy settings and then open a new browser window. At the moment, I'm applying the proxy settings by going into the registry:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");

是进入注册表中的的方式做到这一点,还是有一个比较推荐的方法呢?我想避免更改注册表,如果有一个替代的解决方案。

Is going into the registry the best way to do this, or is there a more recommended approach? I'd like to avoid registry changes if there's an alternative solution.

推荐答案

这一定程度上取决于您的具体需求。如果你正在写一个C#应用程序,只是想设置您的应用程序将使用默认的代理设置,使用类System.Net.GlobalProxySelection(<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx\">http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx).您也可以设置代理与System.Net.WebProxy(<任何特定的连接href=\"http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx\">http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx).

This depends somewhat on your exact needs. If you are writing a C# app and simply want to set the default proxy settings that your app will use, use the class System.Net.GlobalProxySelection (http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx). You can also set the proxy for any particular connection with System.Net.WebProxy (http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx).

如果你真的想更新注册表中的代理服务器设置,我认为你需要使用P / Invoke来调用WinAPI的函数WinHttpSetDefaultProxyConfiguration(<一个href=\"http://msdn.microsoft.com/en-us/library/aa384113.aspx\">http://msdn.microsoft.com/en-us/library/aa384113.aspx).

If you actually want to update the proxy settings in the registry, I believe that you'll need to use P/Invoke to call the WinAPI function WinHttpSetDefaultProxyConfiguration (http://msdn.microsoft.com/en-us/library/aa384113.aspx).

这篇关于编程设置浏览器代理设置在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆