为什么 Process.Start("cmd.exe", process);不行? [英] Why does Process.Start("cmd.exe", process); not work?

查看:28
本文介绍了为什么 Process.Start("cmd.exe", process);不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

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 argument. Try the code below

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

更改为上面的 /K.如果您希望 cmd.exe 在运行命令后关闭,您可以使用 /C 开关.

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

这篇关于为什么 Process.Start("cmd.exe", process);不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆