dotnet,  

winform禁用注册表,任务管理器

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace Ecan
{
    public class EcanSystem
    {

        /// <summary>
        /// 设置程序开机运行
        /// </summary>
        /// <param name="started">是否开机运行</param>
        /// <param name="exeName">要运行的EXE程序名称(不要拓展名)</param>
        /// <param name="path">要运行的EXE程序路径</param>
        /// <returns>成功返回真,否则返回假</returns>

        public bool runWhenStart(bool started, string exeName, string path)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            if (started == true)
            {
                try
                {
                    key.SetValue(exeName, path);//设置为开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                try
                {
                    key.DeleteValue(exeName);//取消开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 解禁任务管理器
        /// </summary>
        /// <returns>成功返回真,否则返回假</returns>

        public bool enableTaskmgr()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
            }
            try
            {
                key.SetValue("disabletaskmgr", 0, RegistryValueKind.DWord);
                key.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 禁用任务管理器
        /// </summary>
        /// <returns>成功返回真,否则返回假</returns>

        public bool notEnableTaskmgr()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
            }
            try
            {
                key.SetValue("disabletaskmgr", 1, RegistryValueKind.DWord);
                key.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 解禁注册表
        /// </summary>
        /// <returns>成功返回真,否则返回假</returns>

        public bool enableRegedit()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
            }
            try
            {
                key.SetValue("disableregistrytools", 0, RegistryValueKind.DWord);
                key.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 禁用注册表
        /// </summary>
        /// <returns>成功返回真,否则返回假</returns>

        public bool notEnableRegedit()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");
            }
            try
            {
                key.SetValue("disableregistrytools", 1, RegistryValueKind.DWord);
                key.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
}
}


留言

您的电子邮箱地址不会被公开。 必填项已用*标注