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

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

问题描述

我正在编写一个需要设置 Internet Explorer 代理设置的 winforms 应用程序,然后打开一个新的浏览器窗口.目前,我正在通过进入注册表来应用代理设置:

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 (http://msdn.microsoft.com/en-us/library/system.net.globalproxyselection.aspx).您还可以为与 System.Net.WebProxy 的任何特定连接设置代理 (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(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天全站免登陆