none
How to force refresh of Windows LAN proxy settings with C#?

    Question

  • I created a Windows service with Visual Studio 2015. My service is for disabling proxy settings for all user accounts. I do this with editing the correct registry key value. It seems to work but the registry key change does not have an effect immediately. Here is my test code:
    private void EditProxyEnable(int proxyEnable, String proxyServer)
        {
            string RegKey = @"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\";
    
            RegistryKey oRegKey = Registry.Users;
            oRegKey = oRegKey.OpenSubKey("X-1-1-11-1111111111-1111111111-1111111111-1111\\" + RegKey, true);
            oRegKey.SetValue("ProxyEnable", proxyEnable);
            oRegKey.SetValue("ProxyServer", proxyServer);
            oRegKey.Close();
    
            bool settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            bool refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    
        }

    Note: X-1-1-11-1111111111-1111111111-1111111111-1111 is a SID for the account that I want to edit its registry key value.

    I try to force the immediate refresh for LAN proxy settings with InternetSetOption method call. With Windows Forms applications this works, but with Windows service it doesn't.

    So is there an alternative way to force the immediate refresh of LAN settings in Windows services?



    Sunday, April 16, 2017 6:52 PM

All replies


  • I try to force the immediate refresh for LAN proxy settings with InternetSetOption method call. With Windows Forms applications this works, but with Windows service it doesn't.

    So is there an alternative way to force the immediate refresh of LAN settings in Windows services?



    The documentation at https://msdn.microsoft.com/en-us/library/windows/desktop/aa385114(v=vs.85).aspx

     states that InternetSetOption should not be used from a service and suggests WinHTTP.  Perhaps you can find the functionality you desire at Setting WinINet Proxy Configurations in WinHTTP

    Sunday, April 16, 2017 7:06 PM
  • Hi PeteH78,

    Thank you for posting here.

    Based on my search, you could refer to the similar thread in SO. And thread link in MSDN forum.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 17, 2017 7:05 AM
    Moderator