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

发布于 2024-11-08 06:00:24 字数 530 浏览 0 评论 0原文

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

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

但是如果 Media Encoder 应用程序正在流式传输或录制,它会在退出时显示一个对话框:

“您确定要停止编码吗?”

那么有没有办法从代码中回答或单击“是”按钮?

[编辑] 许多用户使用 Process.kill() 进行回答,但这不是一个 Option ,因为 Process.Kill(); 将终止 Windows Media Encoder 立即,并且 Windows Media Encoder 将不会最终确定正在写入的文件,这迫使我重新索引视频文件。所以不,我不能使用 Process.Kill();

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 cannot use Process.Kill();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-11-15 06:00:24
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
        }
    }
}
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
        }
    }
}
凉城凉梦凉人心 2024-11-15 06:00:24

迭代 ProcessModule.filename 触发了“访问被拒绝”异常,但是以下代码工作正常。

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文