请有高手帮帮忙,如果您能回答,请在另一贴(网址如下)里同样也发一下答案,这样,我将这两个贴的共计200分,全送给第一个解决问题的朋友。以下是另一贴的问题:http://community.csdn.net/Expert/topic/5172/5172673.xml?temp=.567013改注册表,在一定时间内只能运行指定程序,知道用组策略管理器,可以实现,不过我想用编程的方法。
以下是我的代码,但是不能达到目的,已经研究过N遍了,没有找到问题,请高手,指教。谢谢。第一个解决者,百分送上!
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;namespace restrict
{
    public partial class Form1 : Form
    {
        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");
            
            cu1.SetValue("RestrictRun", 1);
            cu.SetValue("**delvals.", " ");            cu.SetValue("1", "notepad.exe");
            cu.SetValue("2", "bitcomet.exe");
            cu.SetValue("3", "gpedit.msc");
            cu.SetValue("4", "regedit.exe");
            cu.SetValue("5", "tmshell.exe");
            cu.SetValue("6", "kav.exe");
            cu.SetValue("7", "TTraveler.exe");
            cu.SetValue("8", "iexplore.exe");
            cu.SetValue("9", "mmc.exe");
            cu.SetValue("10", "restrict.exe");            hkcu.Close();
                    }
        int delflag = 1;
       public void delres()//删除相关注册表项,清除限制
        {
            RegistryKey hkcu = Registry.CurrentUser;
            RegistryKey hklm = Registry.LocalMachine;
           
            
            RegistryKey cu = hkcu.OpenSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\Policies", true);
            cu.DeleteSubKeyTree("Explorer");
            hkcu.Close();
            delflag = 0;        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            
         if ((DateTime.Now.Hour >= 12)&&(DateTime.Now.Hour <= 14)&&(delflag==1)) //中午12点到下午2点之间,取消限制
            {
                
              delres();
 
            }        }
    }
}

解决方案 »

  1.   

    写个hook,监视打开的是什么程序,如果不是要运行的,就结束他
      

  2.   

    分析:如果设置了注册表就立即生效,那么目前已经运行的程序又怎么办?系统自动将其Kill?不会。
    所以,我认为,应该是当用户重新登录后才对运行的程序限制生效。我没试过,胡说而以
      

  3.   

    楼主可以看看这贴,应该有启发http://topic.csdn.net/t/20011117/12/375080.html
      

  4.   

    to:  raozhiven(朗屹)
    不用注销,设置了,当时就生效。我试过,不过是用组策略管理器。自己的程序是没有反应的。另: raozhiven(朗屹) ( ) 信誉:99    Blog  2006-11-25 14:51:51  得分: 0  
     
     
       楼主可以看看这贴,应该有启发http://topic.csdn.net/t/20011117/12/375080.html
      
     
    ------->对于此页,此兄的问题,我倒是顺便解决了:限制了所有程序,其实并不是,资源管理器还是可以用的,可以在系统目录下找到regedit.exe,将注册表编辑器regedit.exe改名为Regedit.scr。然后,在桌面上点击鼠标右键,在弹出菜单中选择“属性”,在弹出的“显示 属性”对话框中选中“屏幕保护程序”,在“屏幕保护程序”下拉列表框中找到Regedit这个假冒的屏幕保护文件......,后面的不用说也知道了吧
      

  5.   

    但我直接编辑注册表是没有立即见效。那么,应该是需要向 Windows 发送一个通知消息。具体不知道。可以到 msdn 上查查看
      

  6.   

    这是我在博客上写的:我的工作终于完成了!!!以下是全部代码,在vs2005+xp环境下运行通过。以前我发贴问的问题,也解决了,感谢网友们的帮助,特别是raozhiven(朗屹) 的提示,感谢!以前我总是认为代码不对,其实是正确的,主要是居然不知道改了注册表,并不是立即生效的。后来,在网上找资料,发现了,不用重启立即生效的方法。以下是改良过的程序代码。欢迎大家来研讨。更感谢能提出更好方法的朋友。     
    我完成了,并将代码贴到了http://blog.csdn.net/metababy/上。感谢大家的支持。分我会按提示的比例来分给大家。感谢!
      

  7.   

    新工具完成了!!!上班时间只能用指定的部分程序了。唉,有时太过火了~~ 
    我的工具终于完成了!!!以下是全部代码,在vs2005+xp环境下运行通过。以前我发贴问的问题,也解决了,感谢网友们的帮助,特别是raozhiven(朗屹) 的提示,感谢!以前我总是认为代码不对,其实是正确的,主要是居然不知道改了注册表,并不是立即生效的。后来,在网上找资料,发现了,不用重启立即生效的方法。以下是改良过的程序代码。欢迎大家来研讨。更感谢能提出更好方法的朋友。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
               );
            //*******以下为版权信息*******
            //作者:花纯春
            //时间:2006年11月26日
            //请联系我的博客:http://ike.126.com
            //描述:此程序通过修改注册表,实现在工作时间只允许指定程序的运行。在中午休息时间,自动取消限制,可以自由使用。
            //欢迎转载和使用,但请保持版权信息的完整!
            //*******以上为版权信息*******        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();
                // Tell all open programs that this change occurred.
                      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();
                
               // Tell all open programs that this change occurred.
                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)) //中午12点到下午2点之间,取消限制
                {
                    
                  delres();
     
                }        }
        }
    }
     学习在于爱好!