Programmatically Set Browser Proxy Settings in C#
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).
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).
How to change Global Windows Proxy using C# .NET with `Immediate Effect`
The behavior I want is that when ever
I change proxy settings in my app, all
the browsers which are using global
proxy (irrespective of whether they
are running or not) should instantly
incorporate the change in settings.How can I achieve this?
You need to refresh your system to achieve that.
Add these lines at the beginning of your code:
using System.Runtime.InteropServices;
using Microsoft.Win32;
Add this in the beginning of your class:
[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 bool settingsReturn, refreshReturn;
And imply the code:
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", YOURPROXY);
// 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);
How to change LAN Settings (proxy configuration) programmatically
You can change proxy settings by using the registry. See the following link:
http://support.microsoft.com/kb/819961
Key path: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
Values:
"MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyHttp1.1"=dword:00000000
"ProxyServer"="http://ProxyServername:80"
"ProxyOverride"="<local>"
A question in SuperUser.com regarding how to disable automatically detect settings in ie proxy configuration. Disable "Automatically detect settings" in IE proxy configuration
A snippet, taken from Internet Explorer Automatic Configuration Script Definition via Registry.
Script 1: This enables the AutoConf Script and defines what it is (exchange the http://xxxx with your script)
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"AutoConfigURL"="http://xxx.xxx.xxx.xxx.xxxx"
"ProxyEnable"=dword:00000000
Script 2: This script Disables the AutoConf Script and enables a proxy server with exceptions.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"ProxyEnable"=dword:00000001
"ProxyOverride"="proxyexceptionname:portnumber;anotherexceptionname:port
"ProxyServer"="ftp=MyFTPProxy:Port;http=MYHTTPPROXY:PORT;https=MYHTTPSPROXY:PORT
"AutoConfigURL"=""
Programmatically Set Proxy Address, Port, User, Password throught Windows Registry
For IE, you can use the same place in the registry. Just set ProxyServer="user:password@127.0.0.1:8080" however firefox completely rejects this, and does not attempt to connect.
Related Topics
C# - Detect Time of Last User Interaction with the Os
How to Catch a Specific SQLexception Error
Inject Service into Action Filter
What's the Best Way of Accessing Field in the Enclosing Class from the Nested Class
Why Does (Int)(Object)10M Throw "Specified Cast Is Not Valid" Exception
How to Backup and Restore the System Clipboard in C#
Hashing Passwords with Md5 or Sha-256 C#
Difference Between MVC 5 Project and Web API Project
ASP.NET Identity 2 Giving "Invalid Token" Error
Using the Wpf Dispatcher in Unit Tests
Appsettings Get Value from .Config File
Linq to SQL Using Group by and Count(Distinct)
Understanding .Asenumerable() in Linq to SQL
Will the Dynamic Keyword in C#4 Support Extension Methods
Wpf: How to Override Part of a Controltemplate Without Redefining the Whole Style