开发者

Setting Registry Key For All Users In C#

开发者 https://www.devze.com 2022-12-16 15:25 出处:网络
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 Set

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 voi开发者_如何转开发d 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);
    }


Adding it to HKEY_USERS wont be enough(?)


Have you tried using Registry.LocalMachine?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号