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
Why does Process.Start("cmd.exe", process); not work?
提问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.exe
expects a /K
switch 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 /K
above. You can use /C
switch if you want cmd.exe
to 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 /c
or a /k
switch (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);