先获取窗体的进程,然后根据MainWindowTitle名称,判断是否时窗体进程
private void fMain_Load(object sender, EventArgs e)
{
zlist.Items.Clear();
Process[] myProcesses = Process.GetProcesses();
foreach (Process myProcess in myProcesses)
{
if (myProcess.MainWindowTitle.Length > 0)
zlist.Items.Add(myProcess.ProcessName.ToString().Trim());
}
}
通过向窗体发送关闭命名来关闭正在运行的窗体
private void zbtnclose_Click(object sender, EventArgs e)
{
if (zlist.SelectedItem == null) { MessageBox.Show("未选择进程"); return; }
Process[] process = Process.GetProcessesByName(zlist.SelectedItem.ToString().Trim());
foreach (Process node in process)
{
node.CloseMainWindow();
}
MessageBox.Show("已成功发送关闭命令", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}