MrProgram
MrProgram

Reputation: 5242

Getting error trying to start process

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.

Upvotes: 2

Views: 24402

Answers (2)

stanek
stanek

Reputation: 592

I got this error when trying to launch Visual Studio from within Unity. I needed to reboot my computer to finish the Visual Studio installation first.

Upvotes: 1

Shadow
Shadow

Reputation: 4006

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

Upvotes: 5

Related Questions