如何从 C# 关闭另一个进程 [英] How to Close another Process from c#

查看:50
本文介绍了如何从 C# 关闭另一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 C# 应用程序关闭另一个进程(Windows Media Encoder),到目前为止我可以这样做:

I need to close another Process (Windows Media Encoder) from a C# Application ,and so far i can do it with:

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

但如果媒体编码器应用程序正在流式传输或录制,它会在退出时显示一个对话框:

But if the Media Encoder Application is Streaming or Recording it shows a Dialog on exit:

您确定要停止编码吗?"

"Are you sure you want to stop encoding?"

那么有没有办法回答或点击代码中的是"按钮?

So is there a way to answer or click Yes button from Code?

许多用户用 Process.kill() 回答,但这不是一个选项,因为 Process.Kill(); 将终止 Windows Media Encoder 立即,并且 Windows Media Encoder 不会完成正在写入的文件,这迫使我重新索引视频文件.所以我不能使用 Process.Kill();

Many users are answering with Process.kill() ,but that is not an Option ,because 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();

推荐答案

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
        }
    }
}

这篇关于如何从 C# 关闭另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆