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 ProcessKill
{
public partial class Form1 : Form
{
public System.Threading.Thread myThread;
public Form1()
{
InitializeComponent();
myThread = new System.Threading.Thread(new System.Threading.ThreadStart(kill_Process));//初始化线程,执行关闭进程方法kill_Process();
myThread.Start();
}
private void button1_Click(object sender, EventArgs e)
{
GetGetProcesses();
}
/// <summary>
/// 调用此方法可查看本机当前运行的所有进程
/// </summary>
private void GetGetProcesses()
{
listView1.Items.Clear();
Process[] list = Process.GetProcesses(".");//获取本机当前运行的所有进程
for (int i = 0; i < list.Length; i++)//循环添加进程信息
{
listView1.View = View.Details;//列的显示模式
ListViewItem lvi = new ListViewItem(new string[] { list[i].ProcessName, list[i].Id.ToString(), list[i].PrivateMemorySize64.ToString() });
listView1.Items.Add(lvi);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
/// <summary>
/// 关闭进程
/// </summary>
void kill_Process()
{
var killlist = System.Configuration.ConfigurationManager.AppSettings["killname"].ToString().Split(',');//获取app.cong中设置的需要关闭的进程名称
Process[] list = Process.GetProcesses(".");//获取本机当前运行的所有进程
foreach (var item in killlist)//遍历查看进程是否存在
{
foreach (Process pr in list)
{
if (item == pr.ProcessName)
{
pr.Kill();
}
}
}
//Application.Exit();
System.Environment.Exit(System.Environment.ExitCode);//关闭自身进程
}
private void button2_Click(object sender, EventArgs e)
{
kill_Process();
GetGetProcesses();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}