重新启动其他应用程序.C#、.net [英] Restart other application. C#, .net
本文介绍了重新启动其他应用程序.C#、.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想重新启动某个进程.让我们称之为 someApp.exe.如何重新启动该过程?这不是我的应用程序.这是一些外部程序.
I want to restart some process. Lets call it someApp.exe. How can I restart that process? It's not my application. It's some external program.
推荐答案
你想做的是:
- 终止进程
- 重新开始
在 C# 中有一些获取 Process 实例的方法.假设您知道进程的名称:
There are some ways of obtaining a Process instance in C#. Let's suppose you know the name of the process:
var process = Process.GetProcessesByName("notepad++")[0];
然后你可以这样做:
process.Kill();
但是要再次启动,需要知道进程的路径,所以在杀死它之前,保存可执行文件的路径:
But to start it again, you need to know the path of the process, so before killing it, save the path of the executable:
var path = process.MainModule.FileName;
然后你可以这样做:
Process.Start(path);
<小时>您应该在获取第一个元素之前检查 GetProcessesByName
是否返回元素,但我只想关注这里的重要事情.
You should check if
GetProcessesByName
returns elements before taking the first element, but I just wanted to focus on the important thing here.
这篇关于重新启动其他应用程序.C#、.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文