天天看点

C#中用计时器关闭任务管理器

   写这个小程序之前要对Process类有了解,使用这个类首先要命名空间using System.Diagnostics;然后要知道在进程中任务管理器对名字taskmgr,在这个程序中你好还要要使用一个timer事件来监视你想关闭的进程,接下来就是我对代码如果那里不好,放在这里请给出好对建议和意见。

C#中用计时器关闭任务管理器

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

namespace 关闭任务管理器

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        public static  bool  i =  true;

        private void timer1_Tick(object sender, EventArgs e)

        {

            try {

                this.Activate();//设置这个窗体为活动窗体

                Process[] MyProcess = Process.GetProcesses();//定义一个process类的数组并获得所有进程

                foreach (Process p in MyProcess)//使用foreach 方法便利这个数组

                {

                    if (p.ProcessName == "taskmgr")//判断这个数组中有没有这个进程

                    {

                        p.Kill();//杀死进程

                        return;

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(string .Format("{0}",ex.Message));//

            }

        }

        private void button1_Click(object sender, EventArgs e)

        { //有同一个button事件做一个开关

            if (Form1.i== true)

            {

                button1.Text = "开始";

                timer1.Enabled = true;

                Form1.i= false;

            }

           else

            {      button1.Text = "结束";

                    timer1.Enabled = false;

                    Form1.i = true;                   

            }        

        }

    }

}