Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Process.Start("cmd.exe", process); not work?

This works:

Process.Start("control", "/name Microsoft.DevicesAndPrinters");

But this doesn't: (It just opens a command prompt.)

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "control /name Microsoft.DevicesAndPrinters";
Process.Start(info);

Why?

(Yes, I know they're not identical. But the second one "should" work.)

like image 678
ispiro Avatar asked Dec 24 '12 10:12

ispiro


2 Answers

This is because cmd.exe expects a /K switch to execute a process passed as an argument. Try the code below

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/K control /name Microsoft.DevicesAndPrinters";
Process.Start(info);

EDIT: Changed to /K above. You can use /C switch if you want cmd.exe to close after it has run the command.

like image 114
Ravi Y Avatar answered Sep 18 '22 06:09

Ravi Y


You need a /c or a /k switch (options for cmd.exe) so that the command is executed. Try:

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c control /name Microsoft.DevicesAndPrinters";
Process.Start(info);
like image 21
SWeko Avatar answered Sep 22 '22 06:09

SWeko