2011年9月19日 星期一

[C#] Restart a process

According to MSDN:
If the process is already running, no additional process resource is started.
Therefore, we create a ProcessStartInfo object to store FileName and WorkingDirectory, and then we can restart a 'killed' process.

Here is the source code:
public void RestartProcess(Process process)
{
    ProcessStartInfo info = new ProcessStartInfo()
    {
        FileName = process.MainModule.FileName,
        WorkingDirectory = Path.GetDirectoryName(process.MainModule.FileName),
    };
    
    process.Kill();
    process.WaitForExit();

    Process.Start(info);
}
(To format the source code, please refer to here.)

Enjoy!

沒有留言:

張貼留言