You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemProxy.cs 1.7 kB

10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace shadowsocks_csharp
  7. {
  8. class SystemProxy
  9. {
  10. [DllImport("wininet.dll")]
  11. public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  12. public const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
  13. public const int INTERNET_OPTION_REFRESH = 37;
  14. static bool settingsReturn, refreshReturn;
  15. public static void UpdateIE()
  16. {
  17. // These lines implement the Interface in the beginning of program
  18. // They cause the OS to refresh the settings, causing IP to realy update
  19. settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
  20. refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
  21. }
  22. public static void Enable()
  23. {
  24. RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
  25. registry.SetValue("ProxyEnable", 1);
  26. registry.SetValue("ProxyServer", "127.0.0.1:8123");
  27. SystemProxy.UpdateIE();
  28. }
  29. public static void Disable()
  30. {
  31. RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
  32. registry.SetValue("ProxyEnable", 0);
  33. registry.SetValue("ProxyServer", "");
  34. SystemProxy.UpdateIE();
  35. }
  36. }
  37. }