简体   繁体   English

如何使用C#以编程方式删除IE代理

[英]How to remove IE Proxy programmatically using c#

After a lot of searching I've found the following code to successfully set the proxy in the Internet Options pane in Windows: 经过大量搜索之后,我发现以下代码可在Windows的“ Internet选项”窗格中成功设置代理:

[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;

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);            
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:1234");
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

The problem comes when I try to remove the proxy server. 当我尝试删除代理服务器时,问题就来了。 I have tried many things but I can't get it working. 我尝试了很多事情,但无法正常工作。 I have tried the following block of code: 我尝试了以下代码块:

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 0);                     
settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);

I have also tried deleting the "ProxyEnable" key by replacing the set value to 0 in the code block above with the following line: 我还尝试通过以下行将上述代码块中的设置值替换为0来删除“ ProxyEnable”键:

registry.DeleteValue("ProxyEnable");

Can anyone please help with this? 谁能帮忙吗?

I have just found the answer. 我刚刚找到了答案。 Needed to use the following class to correctly marshall the calls to the win api: 需要使用以下类来正确整理对win api的调用:

namespace PoshHttp
{

    public class Proxies
    {
        public static bool UnsetProxy()
        {
            return SetProxy(null, null);
        }
        public static bool SetProxy(string strProxy)
        {
            return SetProxy(strProxy, null);
        }

        public static bool SetProxy(string strProxy, string exceptions)
        {
            InternetPerConnOptionList list = new InternetPerConnOptionList();

            int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3);
            InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
            // USE a proxy server ...
            options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
            options[0].m_Value.m_Int = (int)((optionCount < 2) ? PerConnFlags.PROXY_TYPE_DIRECT : (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
            // use THIS proxy server
            if (optionCount > 1)
            {
                options[1].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER;
                options[1].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
                // except for these addresses ...
                if (optionCount > 2)
                {
                    options[2].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
                    options[2].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
                }
            }

            // default stuff
            list.dwSize = Marshal.SizeOf(list);
            list.szConnection = IntPtr.Zero;
            list.dwOptionCount = options.Length;
            list.dwOptionError = 0;


            int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
            // make a pointer out of all that ...
            IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
            // copy the array over into that spot in memory ...
            for (int i = 0; i < options.Length; ++i)
            {
                IntPtr opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize));
                Marshal.StructureToPtr(options[i], opt, false);
            }

            list.options = optionsPtr;

            // and then make a pointer out of the whole list
            IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize);
            Marshal.StructureToPtr(list, ipcoListPtr, false);

            // and finally, call the API method!
            int returnvalue = NativeMethods.InternetSetOption(IntPtr.Zero,
                                                              InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION,
                                                              ipcoListPtr, list.dwSize) ? -1 : 0;
            if (returnvalue == 0)
            {  // get the error codes, they might be helpful
                returnvalue = Marshal.GetLastWin32Error();
            }
            // FREE the data ASAP
            Marshal.FreeCoTaskMem(optionsPtr);
            Marshal.FreeCoTaskMem(ipcoListPtr);
            if (returnvalue > 0)
            {  // throw the error codes, they might be helpful
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return (returnvalue < 0);
        }
    }

    #region WinInet structures
   [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
   public struct InternetPerConnOptionList
   {
      public int dwSize;               // size of the INTERNET_PER_CONN_OPTION_LIST struct
      public IntPtr szConnection;         // connection name to set/query options
      public int dwOptionCount;        // number of options to set/query
      public int dwOptionError;           // on error, which option failed
      //[MarshalAs(UnmanagedType.)]
      public IntPtr options;
   };

   [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
   public struct InternetConnectionOption
   {
      static readonly int Size;
      public PerConnOption m_Option;
      public InternetConnectionOptionValue m_Value;
      static InternetConnectionOption()
      {
         InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption));
      }

      // Nested Types
      [StructLayout(LayoutKind.Explicit)]
      public struct InternetConnectionOptionValue
      {
         // Fields
         [FieldOffset(0)]
         public System.Runtime.InteropServices.ComTypes.FILETIME m_FileTime;
         [FieldOffset(0)]
         public int m_Int;
         [FieldOffset(0)]
         public IntPtr m_StringPtr;
      }
   }
   #endregion

   #region WinInet enums
   //
   // options manifests for Internet{Query|Set}Option
   //
   public enum InternetOption : uint
   {
      INTERNET_OPTION_PER_CONNECTION_OPTION = 75
   }

   //
   // Options used in INTERNET_PER_CONN_OPTON struct
   //
   public enum PerConnOption
   {
      INTERNET_PER_CONN_FLAGS = 1, // Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags 
      INTERNET_PER_CONN_PROXY_SERVER = 2, // Sets or retrieves a string containing the proxy servers.  
      INTERNET_PER_CONN_PROXY_BYPASS = 3, // Sets or retrieves a string containing the URLs that do not use the proxy server.  
      INTERNET_PER_CONN_AUTOCONFIG_URL = 4//, // Sets or retrieves a string containing the URL to the automatic configuration script.  

   }

   //
   // PER_CONN_FLAGS
   //
   [Flags]
   public enum PerConnFlags
   {
      PROXY_TYPE_DIRECT = 0x00000001,  // direct to net
      PROXY_TYPE_PROXY = 0x00000002,  // via named proxy
      PROXY_TYPE_AUTO_PROXY_URL = 0x00000004,  // autoproxy URL
      PROXY_TYPE_AUTO_DETECT = 0x00000008   // use autoproxy detection
   }
   #endregion

   internal static class NativeMethods
   {
      [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
   }
}

A full description can be found on this blog , I have included the code for reference in case the blog is down in the future. 可以在此博客上找到完整的说明,以防将来博客关闭时提供了参考代码。

You can disable all proxy items as a piece of cake with this code. 您可以使用此代码禁用所有代理项。

RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (registry.GetValue("AutoConfigURL") != null)
{
    registry.DeleteValue("AutoConfigURL");
}
registry.SetValue("ProxyEnable", 0, RegistryValueKind.DWord);
registry.SetValue("ProxyServer", "");
registry.SetValue("MigrateProxy", 0, RegistryValueKind.DWord);
registry.SetValue("ProxyHttp1.1", 0, RegistryValueKind.DWord);
registry.SetValue("ProxyOverride", "");
registry.Flush();

Wish I could help you. 希望我能为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM