为什么的Process.Start(" CMD.EXE",流程);不行?流程、Start、Process、QUOT

2023-09-04 22:38:44 作者:留不住得人像阵风

本作品:

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

为什么?

(是的,我知道他们是不相同的,但第二个应当的工作。)

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

推荐答案

这是因为CMD.EXE需要一个/ K开关来执行通过为实际参数的一个过程。尝试下面

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

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

编辑:更改为/ K以上。您可以使用/ C开关,如果你想的CMD.exe关闭后,已运行的命令。

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