C#检测进程退出进程

2023-09-03 01:18:58 作者:倾城如梦未必阑珊

我有以下code:

   private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        System.Diagnostics.Process execute = new System.Diagnostics.Process();

        execute.StartInfo.FileName = e.FullPath;
        execute.Start();

        //Process now started, detect exit here

    }

在FileSystemWatcher的是看哪里的.exe文件都得到保存到一个文件夹中。这得到了保存到该文件夹​​中的文件被正确执行。但打开exe文件关闭时,另一功能应该被触发。

The FileSystemWatcher is watching a folder where .exe files are getting saved into. The files which got saved into that folder are executed correctly. But when the opened exe closes another function should be triggered.

有没有一种简单的方法来做到这一点?

Is there a simple way to do that?

推荐答案

过程。 WaitForExit 。

顺便一提,因为进程工具的IDisposable ,你真正想要的:

And incidentally, since Process implements IDisposable, you really want:

using (System.Diagnostics.Process execute = new System.Diagnostics.Process())
{
    execute.StartInfo.FileName = e.FullPath; 
    execute.Start(); 

    //Process now started, detect exit here 
}