C# – Getting error trying to start process

cprocess

foreach (var process in Process.GetProcessesByName("SnippingTool"))
{
    process.Kill();
    Thread.Sleep(5000);
    process.Start();
}

I'm trying to restart a .exe using Process. Why isn't this working? Thought it should be able to find the process since it success killing it.

Getting this error:

System.InvalidOperationException: Cannot start process because a file
name has not been provided

EDIT: To clarify, the process.Kill works, it's the Start() that gets exception.

Best Answer

The problem is you aren't getting the file location of the process, and it doesn't know how to start it. Doing the following will work, however if you are running a 64-bit machine, you should make sure that you compile your program in 64-bit (Project Properties -> Build -> Platform Target: x64)enter image description here

foreach (Process process in Process.GetProcessByName("SnippingTool"))
{
    string fullPath = process.MainModule.FileName;
    process.Kill();
    Thread.Sleep(5000);
    Process.Start(fullPath);
}

I've tested this and it worked for me