Reputation: 21
Im trying to be close the calculator when the user press's a key on the key board. But p.kill and p.CloseMainWindow doesn't kill the calculator, only the shell which is executed.
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "/c calc ";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.Start();
Console.WriteLine("Press any key to kill Calc");
Console.ReadKey();
p.CloseMainWindow();
p.Kill();
Upvotes: 2
Views: 9662
Reputation: 46008
You need to find the Calculator process and kill it. There are actually two processes created: one for the cmd
and the other for Calculator. You are killing only the first one.
The other solution is to start the Calculator directly, without using cmd
.
Upvotes: 3
Reputation: 44595
because your process is not the calc.exe process but the command prompt which executes the calc.
to find a process by name and kill it, you should use GetProcessByName,
see an example here: C# Process Process.GetProcessesByName, Kill Process and Exit Event
Upvotes: 0
Reputation: 9639
Don't use the shell (cmd) but run the calc process directly. Setting Process.StartInfo.FileName to "calc" should do it (assuming calc.exe is on the system path).
Upvotes: 7