退出或结束其他可执行文件

时间:2011-08-11 11:04:14

标签: c#

我有这个代码来运行exe:

String cPath = "C:\\GCOS\\HHT\\EXE\\" + frmSchemas.schema;
string cParams = HHTNUMBER+" "+ Login.user + "/" + Login.pass + "//" +Login.db + "//" + frmSchemas.schema ;
string filename = Path.Combine(cPath,"HHTCtrlp.exe"); 
Process.Start(filename, cParams); 

现在我如何结束上面的程序?

10 个答案:

答案 0 :(得分:8)

        Process[] processes = Process.GetProcessesByName("HHTCtrlp");
        foreach (var process in processes)
        {
            process.Kill();
        }

答案 1 :(得分:2)

以下是http://csharp-slackers.blogspot.com/2008/09/terminate-process.html

的示例
using System;
using System.Threading;
using System.Diagnostics;

public class TerminateProcessExample {

public static void Main () {

    // Create a new Process and run notepad.exe.
    using (Process process = Process.Start("notepad.exe")) {

        // Wait for 5 seconds and terminate the notepad process.
        Console.WriteLine("Waiting 5 seconds before terminating" +
            " notepad.exe.");
        Thread.Sleep(5000);

        // Terminate notepad process.
        Console.WriteLine("Terminating Notepad with CloseMainWindow.");

        // Try to send a close message to the main window.
        if (!process.CloseMainWindow()) {

            // Close message did not get sent - Kill Notepad.
            Console.WriteLine("CloseMainWindow returned false - " +
                " terminating Notepad with Kill.");
            process.Kill();

        } else {

            // Close message sent successfully; wait for 2 seconds
            // for termination confirmation before resorting to Kill.
            if (!process.WaitForExit(2000)) {

                Console.WriteLine("CloseMainWindow failed to" +
                    " terminate - terminating Notepad with Kill.");
                process.Kill();
            }
        }
    }

    // Wait to continue.
    Console.WriteLine("Main method complete. Press Enter.");
    Console.ReadLine();
}
}

正如您所看到的,除了使用Process.Kill();

之外,还有更优雅的方法来尝试终止流程

答案 2 :(得分:1)

Process.Start 将返回进程实例。您可以在实例上调用Kill来终止该过程。

答案 3 :(得分:0)

将您的流程分配给变量并调用Kill方法。即。

String cPath = "C:\\GCOS\\HHT\\EXE\\" + frmSchemas.schema;
string cParams = HHTNUMBER+" "+ Login.user + "/" + Login.pass + "//" +Login.db + "//" + frmSchemas.schema ;
string filename = Path.Combine(cPath,"HHTCtrlp.exe"); 
var p = Process.Start(filename, cParams); 
// ... Later in Code ...
p.Kill();

答案 4 :(得分:0)

您可以保持您开始的流程:

var example_process = Process.Start("notepad.exe");

后来:

example_process.Kill();

答案 5 :(得分:0)

Process p = Process.Start(filename, cParams);
...
p.Kill();

答案 6 :(得分:0)

Process proc = Process.Start(filename, cParams); 
 // ....
proc.CloseMainWindow();
proc.Close();
// ...or the rude way ;) ...
proc.Kill();

答案 7 :(得分:0)

保持对流程的处理 - Process.Start返回Process个对象。

然后你可以使用(在极端情况下):

process.Kill();

停止它。

使用:

process.CloseMainWindow();

可能是一种更好的方法(假设该过程具有UI)

答案 8 :(得分:0)

您需要进程ID才能将其终止。

foreach (Process proc in Process.GetProcesses()) 
{
  if (proc.Id == _processID) 
  {
    proc.Kill();
  }
}

答案 9 :(得分:0)

System.Diagnostics.Process.Start("c:\\windows\\system32\\notepad.exe");
System.Diagnostics.Process q;
q = System.Diagnostics.Process.GetProcessesByName("notepad")[0];
q.Kill();