Skip to content

Instantly share code, notes, and snippets.

@ye4241
Created September 21, 2017 05:20
Show Gist options
  • Save ye4241/c47ffc6c12fdc4f68dd8d967f42ff3fa to your computer and use it in GitHub Desktop.
Save ye4241/c47ffc6c12fdc4f68dd8d967f42ff3fa to your computer and use it in GitHub Desktop.
Change IE proxy via C#
[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;
Registry.SetValue(keyName, "ProxyServer", proxyhost);
Registry.SetValue(keyName, "ProxyEnable", proxyEnabled?"1": "0");
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment