C# 为什么 Process.Start("cmd.exe", process); 不行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14020109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:25:55  来源:igfitidea点击:

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

c#.netcommand-line

提问by ispiro

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

(是的,我知道它们并不相同。但第二个“应该”起作用。)

采纳答案by ryadavilli

This is because cmd.exeexpects a /Kswitch to execute a process passed as an argument. Try the code below

这是因为cmd.exe期望/K开关执行作为参数传递的进程。试试下面的代码

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

EDIT: Changed to /Kabove. You can use /Cswitch if you want cmd.exeto close after it has run the command.

编辑:更改为/K上面。/C如果要cmd.exe在运行命令后关闭,可以使用switch 。

回答by yogi

Try this one

试试这个

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

回答by SWeko

You need a /cor a /kswitch (options for cmd.exe) so that the command is executed. Try:

您需要一个/c或 一个/k开关(选项cmd.exe)以便执行命令。尝试:

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