开发者

How to Close another Process from c# [duplicate]

This question already has answers here: Control another application using C# (6 answers) Closed 1 year ago.

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?

[Edit] 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开发者_如何学C 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
        }
    }
}


Iterating through ProcessModule.filename triggered "Access denied" exception, however the following code worked fine.

Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
    if (process.ProcessName == PROC_NAME)
        process.CloseMainWindow();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜