Registry settings immediate effect using C#

2020-02-15 02:39发布

问题:

I have used the following code to disable the control panel:

RegistryKey RegKey = Registry.CurrentUser.CreateSubKey(
    @"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer");
RegKey.SetValue("NoControlPanel", false, RegistryValueKind.DWord); 
RegKey.Close();

The above code disables control panel only after restarting, I would like to apply the setting immediately without restarting. Please help me.

回答1:

Try this...

private const int HWND_BROADCAST = 0xffff;
private const int WM_WININICHANGE = 0x001a, WM_SETTINGCHANGE = WM_WININICHANGE, INI_INTL = 1;

SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, INI_INTL);

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint wMsg, uint wParam, uint lParam);

This will notify all applications that changes have been made to the registry, and those programs that accept the notification shuould reload their settings.

Note that not all applications may do this, but things like control panel should.



回答2:

I haven't tested this, but I suspect you only have to close all your explorer.exe processes for this to take effect.

Note that the desktop and taskbar are provided by explorer.exe, so you'll need to start a new one after closing them all.

It's a bit hostile, because the user might have Explorer windows that they don't want to lose, so do it only if it's not going to annoy people. 8-)