【问题标题】:Run cmd.exe with C#,needs to input "Y" [duplicate]用C#运行cmd.exe,需要输入“Y” [重复]
【发布时间】:2019-07-29 03:05:26
【问题描述】:

我想用 System.Diagnostics.Process 运行 cmd 命令:“net use * /delete”,但命令需要输入“Y”或“N”。

这是我的代码:

Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
string dosLine = "/C echo y | net use * /delete";
proc.StartInfo.Arguments = dosLine;
proc.Start();

代码不起作用。 我也试过这个:

proc.StandardInput.WriteLine("net use * /delete");
proc.StandardInput.WriteLine("Y"); 

还是不行

我该怎么办?

【问题讨论】:

标签: c#


【解决方案1】:

net use 带有 /y 标志,因此您可以直接传递它。你不需要输入它。你也可以像这样简化你的代码:

Process proc = new Process();
proc.StartInfo.FileName = "net";
proc.StartInfo.Arguments = "use * /delete /y";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2013-07-27
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多