Why does Process.Start(“cmd.exe”, process); not wo

2020-02-01 00:49发布

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.)

3条回答
爷、活的狠高调
2楼-- · 2020-02-01 01:03

Try this one

ProcessStartInfo info = new ProcessStartInfo("control");
info.Arguments = "/name Microsoft.DevicesAndPrinters";
Process.Start(info);
查看更多
做个烂人
3楼-- · 2020-02-01 01:04

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);
查看更多
戒情不戒烟
4楼-- · 2020-02-01 01:15

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.

查看更多
登录 后发表回答