修改环境变量

好文章,收藏备用, 引用自 http://blog.csdn.net/metababy/archive/2006/11/26/1415624.aspx

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

        }
    }
}


 学习在于爱好!

posted on 2007-05-30 22:08  xc#  阅读(634)  评论(1编辑  收藏  举报