using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace 关闭正在运行的程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetProcess();
}
//获取计算机的进程列表
private void GetProcess()
{
listBox1.Items.Clear();
Process[] myProcesses = Process.GetProcesses(); //获取计算机的进程列表
foreach (Process myProcess in myProcesses) //遍历所允许的程序
{
if (myProcess.MainWindowTitle.Length > 0) //获取运行程序标题栏的信息是否>0
listBox1.Items.Add(myProcess.ProcessName.ToString().Trim()); //把获取进程的名称添加到ListBox
}
}
//关闭事件
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count ==1)
{
Process[] myProcesses = Process.GetProcessesByName(listBox1.SelectedItem.ToString().Trim()); //获取要关闭的进程名称
foreach (Process myProcess in myProcesses)
{
myProcess.CloseMainWindow();
//myProcess.Kill(); //关闭运行的程序
}
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
MessageBox.Show("进程已关闭", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//刷新事件
private void button2_Click(object sender, EventArgs e)
{
GetProcess();
}
}
}