0

I want to run cmd command:"net use * /delete" with System.Diagnostics.Process,but the command needs to input "Y" or "N".

here is my code:

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();

the codes do not work. i also tried this:

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

still not work

what should i do?

rick
  • 1
  • Please refer to https://stackoverflow.com/questions/13284106/why-net-use-delete-does-not-work-but-waits-for-confirmation-in-my-powershel – Miller Cy Chan Jul 29 '19 at 03:11

1 Answers1

-1

net use takes a /y flag, so you can just pass that instead. You don't need to input it. You could also simplify your code like so:

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();
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86