新工具完成了!!!上班时间只能用指定的部分程序了。唉,有时太过火了~~

我的工具终于完成了!!!以下是全部代码,在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();
 
            }

        }
    }
}


 学习在于爱好!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值