简体   繁体   English

如何从c#关闭另一个进程

[英]How to Close another Process from c#

I need to close another Process (Windows Media Encoder) from a C# Application ,and so far i can do it with: 我需要从C#应用程序关闭另一个进程(Windows Media Encoder),到目前为止我可以使用:

Process.GetProcessesByName("wmenc.exe")[0].CloseMainWindow();

But if the Media Encoder Application is Streaming or Recording it shows a Dialog on exit: 但是如果Media Encoder Application是Streaming或Recording,它会在退出时显示一个Dialog:

"Are you sure you want to stop encoding?" “你确定要停止编码吗?”

So is there a way to answer or click Yes button from Code? 那么有没有办法回答或点击代码中的是按钮?

[Edit] Many users are answering with Process.kill() ,but that is not an Option ,because Process.Kill(); [编辑]许多用户正在回答Process.kill() ,但这不是Option,因为Process.Kill(); will Terminate Windows Media Encoder immediately ,and Windows Media Encoder will not Finalize the File which is Writing ,which forces me to Reindex the Video File .So no i cannot use Process.Kill(); 将立即终止Windows Media EncoderWindows Media Encoder将不会完成正在写入的文件,这迫使我重新索引视频文件。所以我不能使用Process.Kill();

Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
    // now check the modules of the process
    foreach (ProcessModule module in process.Modules)
    {
        if (module.FileName.Equals("MyProcess.exe"))
        {
            process.Kill();
        } else 
        {
         enter code here if process not found
        }
    }
}

Here's an SO post which asks and answers how to control other applications from .NET. 这是一篇SO帖子,询问并回答如何从.NET 控制其他应用程序。 The accepted answer uses just four lines of code to get a window and send a key press to a particular button in that window. 接受的答案仅使用四行代码来获取窗口并将按键发送到该窗口中的特定按钮。 I think that may be your best bet here for cleanly closing the application. 我认为这可能是你干净地关闭应用程序的最佳选择。

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

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