Setting Registry Key For All Users In C#

Deadly 提交于 2019-12-24 03:12:24

问题


I have an application that changes some registry values during installation.

I am changing ProxyEnable and ProxyServer in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings.

This works great when installing as "Just Me" in the .NET installer however I would like to set these values for all users on the computer (Everyone).

My application is a proxy server that will log all URL requests that it receives. For this to work it requires the proxy values to be setup in Internet Settings. I would like this to happen as part of the install process instead of the admin having to set it for all users.

I know this can be done with Group Policy but some machines that will use this application will have multiple users and no Group Policy (XP Home, etc.).

Is there a way to change the mentioned Registry Keys so that all user's IE will have the Prxy settings set?

The code I am currently using is:

    private void EnableProxy(string proxy) {
        using(RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) {
            registry.SetValue("ProxyEnable", 1);
            registry.SetValue("ProxyServer", proxy);
        }

        settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED,
            IntPtr.Zero, 0);
        refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    }

    private void DisableProxy() {
        using(RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) {
            registry.SetValue("ProxyEnable", 0);
            registry.DeleteValue("ProxyServer", false);
        }

        settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED,
            IntPtr.Zero, 0);
        refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
    }

回答1:


Adding it to HKEY_USERS wont be enough(?)




回答2:


Have you tried using Registry.LocalMachine?

http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.localmachine.aspx




回答3:


I found the answer with help from http://www.pctools.com/guides/registry/detail/1147/.

I needed to create ProxySettingsPerUser and set it to 0 then set the ProxyEnable and ProxyServer for the LocalMachine.



来源:https://stackoverflow.com/questions/2088221/setting-registry-key-for-all-users-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!