简体   繁体   English

重启其他应用程序C#,. net

[英]Restart other application. C#, .net

I want to restart some process. 我想重启一些过程。 Lets call it someApp.exe. 让我们称之为someApp.exe。 How can I restart that process? 如何重新启动该过程? It's not my application. 这不是我的申请。 It's some external program. 这是一些外部程序。

What you want to do is: 你想要做的是:

  • Kill the process 杀死这个过程
  • Start it again 再次启动它

There are some ways of obtaining a Process instance in C#. 有一些方法可以在C#中获取Process实例。 Let's suppose you know the name of the process: 假设您知道进程的名称:

var process = Process.GetProcessesByName("notepad++")[0];

Then you can do: 然后你可以这样做:

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;

And then you can do: 然后你可以这样做:

Process.Start(path);


You should check if GetProcessesByName returns elements before taking the first element, but I just wanted to focus on the important thing here. 您应该在获取第一个元素之前检查GetProcessesByName返回元素,但我只想关注重要的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM