某些windows设置的时候,我们如果通过代码设置的时候,通常是修改一些注册表项,可是每次都不是立即生效,查了半天发现原来还需要调一个api通知所有窗体,经测试好使,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace restrict
{
public partial class Form1 : Form
{
int delflag = 1;
IntPtr result1;
// SendMessageTimeout tools
[Flags]
public enum SendMessageTimeoutFlags : uint
{
SMTO_NORMAL = 0x0000,
SMTO_BLOCK = 0x0001,
SMTO_ABORTIFHUNG = 0x0002,
SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
}
const int WM_SETTINGCHANGE = 0x001A;
const int HWND_BROADCAST = 0xffff;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr windowHandle,
uint Msg,
IntPtr wParam,
IntPtr lParam,
SendMessageTimeoutFlags flags,
uint timeout,
out IntPtr result
);
public Form1()
{
InitializeComponent();


RegistryKey hkcu = Registry.CurrentUser;
RegistryKey cu = hkcu.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Policies/Explorer/RestrictRun");
RegistryKey cu1 = hkcu.CreateSubKey(@"SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/Policies/Explorer");
RegistryKey cu2 = hkcu.OpenSubKey(@"SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/RUN", true);
string aa = Application.ExecutablePath;
cu2.SetValue("RestrictFun", aa);

cu1.SetValue("RestrictRun", 1);
cu.SetValue("1", "notepad.exe");
cu.SetValue("2", "gpedit.msc");
cu.SetValue("3", "regedit.exe");
cu.SetValue("4", "tmshell.exe");
cu.SetValue("5", "kav.exe");
cu.SetValue("6", "TTraveler.exe");
cu.SetValue("7", "iexplore.exe");
cu.SetValue("8", "mmc.exe");
cu.SetValue("9", "restrict.exe");
hkcu.Close();
SendMessageTimeout(
new IntPtr(HWND_BROADCAST), 
WM_SETTINGCHANGE, 
IntPtr.Zero, 
IntPtr.Zero,
SendMessageTimeoutFlags.SMTO_NORMAL,
1000,
out result1);

}


public void delres()
{
RegistryKey hkcu = Registry.CurrentUser;
RegistryKey cu = hkcu.OpenSubKey(@"SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/Policies/Explorer", true);
cu.DeleteValue("RestrictRun");
cu.DeleteSubKey("RestrictRun");
hkcu.Close();

SendMessageTimeout(
new IntPtr(HWND_BROADCAST),
WM_SETTINGCHANGE,
IntPtr.Zero,
IntPtr.Zero,
SendMessageTimeoutFlags.SMTO_NORMAL,
1000,
out result1);
delflag = 0;
}

private void timer1_Tick(object sender, EventArgs e)
{

if ((DateTime.Now.Hour >= 12)&&(DateTime.Now.Hour <= 14)&&(delflag==1)) 
{

delres();

}
}
}
}

  

posted on 2012-07-26 20:27  二豆  阅读(2635)  评论(0编辑  收藏  举报