简体   繁体   English

c# 终止由 Process.Start("explorer.exe") 创建的进程

[英]c# Kill process created by Process.Start("explorer.exe")

i want open(or create process) directory specific location and close(or kill process)我想打开(或创建进程)目录特定位置并关闭(或终止进程)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Process ps;

        private void button1_Click(object sender, EventArgs e)
        {
            ps = Process.Start("explorer.exe", @"C:\test");
            MessageBox.Show(ps.Id.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ps.Kill();
        }
    }
}

but an error occurred "Cannot process request because the process has exited."但出现错误“无法处理请求,因为进程已退出。”


edit编辑

i want create process ("explorer.exe C:\test") (click button 1) and kill process made infront of (click button 2).我想要创建进程 ("explorer.exe C:\test")(单击按钮 1)并终止前面的进程(单击按钮 2)。 "click button 1" then "click button 2". “单击按钮 1”,然后“单击按钮 2”。

i predict "Process ps = Process.Start("explorer.exe C:\test");"我预测“Process ps = Process.Start("explorer.exe C:\test");” can kill like "ps.Kill()".可以像“ps.Kill()”一样杀死。

but already ps(Process variable) is disappear(killed?) and can't kill created directory process("explorer.exe",@"C:\test").但 ps(进程变量)已经消失(被杀死?)并且无法杀死创建的目录进程(“explorer.exe”,@“C:\test”)。

how to kill i created(or start) explorer.exe process如何杀死我创建(或启动)explorer.exe 进程


how to kill process created by Process.Start("explorer.exe")?如何终止 Process.Start("explorer.exe") 创建的进程?

Please try this code.请尝试此代码。

Process processToKill = Process.GetProcessById(ps.Id);
processToKill.Kill();

You can use Process.GetProcessById() to get the currently running processes, then Process.Kill() to kill a process.您可以使用Process.GetProcessById()获取当前正在运行的进程,然后使用Process.Kill()进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM