在 C# 中以编程方式终止 vista/windows 7 中的进程
我想在 vista/windows 7 中以编程方式终止一个进程(我不确定两者之间的 UAC 实现是否存在重大问题以产生影响)。
现在,我的代码如下所示:
if(killProcess){
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProcessName");
// Before starting the new process make sure no other MyProcessName is running.
foreach (System.Diagnostics.Process p in process)
{
p.Kill();
}
myProcess = System.Diagnostics.Process.Start(psi);
}
我必须这样做,因为我需要确保如果用户使程序崩溃或突然退出,则当应用程序重新启动时,或者如果用户想要更改参数,会重新启动此辅助进程对于这个二次过程。
该代码在 XP 中运行良好,但在 Windows 7 中(我假设在 Vista 中)失败,并显示“访问被拒绝”消息。 根据全能的谷歌告诉我的,我需要以管理员身份运行我的杀戮程序来解决这个问题,但这只是弱酱。 另一个可能的答案是使用 LinkDemand,但我不明白 LinkDemand 的 msdn 页面,因为它与进程有关。
我可以将代码移动到一个线程中,但这有很多我真的不想发现的固有的其他困难。
I want to kill a process programmatically in vista/windows 7 (I'm not sure if there's significant problems in the implementation of the UAC between the two to make a difference).
Right now, my code looks like:
if(killProcess){
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProcessName");
// Before starting the new process make sure no other MyProcessName is running.
foreach (System.Diagnostics.Process p in process)
{
p.Kill();
}
myProcess = System.Diagnostics.Process.Start(psi);
}
I have to do this because I need to make sure that if the user crashes the program or exits abruptly, this secondary process is restarted when the application is restarted, or if the user wants to change the parameters for this secondary process.
The code works fine in XP, but fails in Windows 7 (and I assume in Vista) with an 'access is denied' message. From what the Almighty Google has told me, I need to run my killing program as administrator to get around this problem, but that's just weak sauce. The other potential answer is to use LinkDemand, but I don't understand the msdn page for LinkDemand as it pertains to processes.
I could move the code into a thread, but that has a whole host of other difficulties inherent to it that I really don't want to discover.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是对的,因为您没有管理权限。 您可以通过在本地系统用户下安装服务并根据需要对其运行自定义命令来解决此问题。
在您的 Windows 窗体应用程序中:
在您的服务中:
You are correct in that it's because you don't have administrative priveleges. You can solve this by installing a service under the local system user and running a custom command against it as needed.
In your windows form app:
In your service:
我将根据 Simon Buchan 的建议添加代码。 假设您的 Windows 窗体是首先启动该过程的,那么这是有道理的并且应该也可以工作。
这是您创建流程的地方。 注意变量 myProc。 这就是你对它的处理:
稍后,只需用以下命令杀死它:
I'll add the code for Simon Buchan's suggestion. It makes sense and should work as well, assuming your windows form is what launched the process in the first place.
Here's where you create the process. Notice the variable myProc. That's your handle on it:
Later, just kill it with: