Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么Process.Start(“cmd.exe”,Process);不工作?_C#_.net_Command Line - Fatal编程技术网

C# 为什么Process.Start(“cmd.exe”,Process);不工作?

C# 为什么Process.Start(“cmd.exe”,Process);不工作?,c#,.net,command-line,C#,.net,Command Line,这项工作: Process.Start("control", "/name Microsoft.DevicesAndPrinters"); 但事实并非如此:(它只是打开了一个命令提示符。) 为什么? (是的,我知道它们不一样。但是第二个“应该”起作用。)试试这个 ProcessStartInfo info = new ProcessStartInfo("control"); info.Arguments = "/name Microsoft.DevicesAndPrinters"; Proce

这项工作:

Process.Start("control", "/name Microsoft.DevicesAndPrinters");
但事实并非如此:(它只是打开了一个命令提示符。)

为什么?

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

试试这个

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

这是因为
cmd.exe
需要一个
/K
开关来执行作为参数传递的进程。请尝试下面的代码

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

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

您需要
/C
/k
开关(用于
cmd.exe
的选项),以便执行命令。尝试:

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

这对我来说很好。你的错误是什么?@SonerGönül没有错误。正如我所写的,它只是打开一个命令提示符。它应该打开设备和打印机。(你的意思是它能帮你吗?)我知道。但我特别希望看到命令行和结果。
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c control /name Microsoft.DevicesAndPrinters";
Process.Start(info);