Restart other application. C#, .net

♀尐吖头ヾ 提交于 2020-01-31 12:07:56

问题


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.


回答1:


What you want to do is:

  • Kill the process
  • Start it again

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];

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.


回答2:


You can use the Process.Start.

http://msdn.microsoft.com/en-us/library/53ezey2s.aspx#Y1400



来源:https://stackoverflow.com/questions/5954033/restart-other-application-c-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!