使用C#运行cmd.exe,需要输入;“是”;

使用C#运行cmd.exe,需要输入;“是”;,c#,C#,我想用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.Redi

我想用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"); 
还是不行


我该怎么办?

网络使用
使用一个
/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();
请参阅