我正在编写一个winforms应用程序,它需要设置的代理设置,然后打开一个新的浏览器窗口。目前,我正在通过进入注册表应用代理设置:
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");
是否进入注册表是最好的方法,还是有更多的推荐方法?如果有其他解决方案,我想避免注册表更改。
发布于 2008-10-16 15:34:34
这在一定程度上取决于您的确切需求。如果您正在编写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)的任何特定连接设置代理。
如果您真的想更新注册表中的代理设置,我相信您需要使用P/Invoke来调用WinAPI函数WinHttpSetDefaultProxyConfiguration (http://msdn.microsoft.com/en-us/library/aa384113.aspx)。
发布于 2010-12-20 20:20:52
来自:http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/19517edf-8348-438a-a3da-5fbe7a46b61a
在代码的开头添加以下行:
使用System.Runtime.InteropServices;使用Microsoft.Win32;
[DllImport("wininet.dll")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
bool settingsReturn, refreshReturn;
并暗示守则:
RegKey.SetValue("ProxyServer", YOURPROXY);
RegKey.SetValue("ProxyEnable", 1);
// These lines implement the Interface in the beginning of program
// They cause the OS to refresh the settings, causing IP to realy update
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
发布于 2014-10-09 07:52:33
我写了一个10行的程序来做这件事,你可以试试https://github.com/131/proxytoggle
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace ProxyToggle
{
class Program
{
[DllImport("wininet.dll")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
public const int INTERNET_OPTION_REFRESH = 37;
static void setProxy(string proxyhost, bool proxyEnabled)
{
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
const string keyName = userRoot + "\\" + subkey;
if(proxyhost.Length != 0)
Registry.SetValue(keyName, "ProxyServer", proxyhost);
Registry.SetValue(keyName, "ProxyEnable", proxyEnabled ? "1" : "0", RegistryValueKind.DWord);
// These lines implement the Interface in the beginning of program
// They cause the OS to refresh the settings, causing IP to realy update
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
static void Main(string[] args)
{
if (args.Length == 0)
{
setProxy("", false);
return;
}
setProxy(args[0], true);
}
}
}
https://stackoverflow.com/questions/197725
复制相似问题