我如何杀死使用Vb.NET或C#的过程? [英] How do I kill a process using Vb.NET or C#?

查看:150
本文介绍了我如何杀死使用Vb.NET或C#的过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我要检查用户是否已经打开Microsoft Word中。如果他有,那么我要杀死Winword.exe进程,并继续执行我的code。

I have a scenario where I have to check whether user has already opened Microsoft Word. If he has, then I have to kill the winword.exe process and continue to execute my code.

任何人不会有任何直接的code使用vb.net或C#杀死一个进程?

Does any one have any straight-forward code for killing a process using vb.net or c#?

推荐答案

您将要使用System.Diagnostics.Process.Kill方法。你可以得到你想要使用过程
<一href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname.aspx\">System.Diagnostics.Proccess.GetProcessesByName.

You'll want to use the System.Diagnostics.Process.Kill method. You can obtain the process you want using System.Diagnostics.Proccess.GetProcessesByName.

例子已经张贴在这里,但我发现,非.exe文件的版本更好地工作,所以是这样的:

Examples have already been posted here, but I found that the non-.exe version worked better, so something like:

foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("winword") )
{
    try
    {
        p.Kill();
        p.WaitForExit(); // possibly with a timeout
    }
    catch ( Win32Exception winException )
    {
        // process was terminating or can't be terminated - deal with it
    }
    catch ( InvalidOperationException invalidException )
    {
        // process has already exited - might be able to let this one go
     }
}

您可能没有处理引发NotSupportedException ,这表明这个过程是远程的。

You probably don't have to deal with NotSupportedException, which suggests that the process is remote.

这篇关于我如何杀死使用Vb.NET或C#的过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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