【问题标题】:Can't kill a process with name with spaces无法杀死名称带空格的进程
【发布时间】:2015-11-07 03:12:48
【问题描述】:

我在我的主类中得到了这个(程序是一个补丁程序 exe..):

private void Form1_Load(object sender, EventArgs e)
{

    // in the exe.dat is written this(the name of the running exe file): KF2 DSM.exe
    string FileName = File.ReadAllText("exe.dat"); 

    // this SHOULD kill the process BUT it doesn't! btw i also treid this: Process.Start("taskkill", "/F /IM " + '"' + FileName + '"');, and still nothing
    Process.Start("taskkill", "/F /IM " + FileName); 

    File.Delete(FileName);

    using (var client = new WebClient())
    {
        client.DownloadFile("https://onedrive.live.com/download?resid=763D7D60E7D1759D!328&authkey=!AArR3IwAehnZ3gc&ithint=file%2cexe", FileName);

        while(client.IsBusy)
        {
            Thread.Sleep(500);
        }
    }

    File.Delete("exe.dat");

    Process.Start(FileName);
}

我在代码中为你添加了一些注释。

我尝试了几乎所有用于终止进程的语法/代码,但都没有奏效!

还有其他方法可以杀死对我有用的进程吗?

【问题讨论】:

  • 您需要转义空格。否则,它会显示为 taskkill 的两个参数。另外,不要为taskkill付出代价。查看 Process 类的其他方法。
  • 我之前都试过了
  • 在进程名之间使用\"允许空格,它基本上是创建一个参数而不是多个用空格分隔的参数。
  • Process.Start("taskkill", "/F /IM \"" + FileName+"\"");

标签: c# kill-process


【解决方案1】:

使用Process.GetProcessesByNameProcess.Kill

foreach (var process in Process.GetProcessesByName(FileName)) {
    process.Kill();
}

【讨论】:

  • 嗯嗯。它以什么方式不起作用?错误?找不到进程?不会杀死进程吗?
猜你喜欢
  • 2011-02-25
  • 2011-06-19
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 2014-05-18
  • 2013-08-05
  • 2015-06-08
  • 2015-06-01
相关资源
最近更新 更多